Orchestrator¶
The agent loop, tool dispatch, and provider discovery.
apab.agent.orchestrator ¶
Agent orchestrator: LLM ↔ tool-calling loop.
AgentOrchestrator ¶
Drives the agentic LLM ↔ tool loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ProjectConfig
|
Project configuration. |
required |
workspace
|
Workspace | None
|
Workspace manager for run bundles. |
None
|
provider
|
LLMProvider | None
|
An already-instantiated LLM provider. If |
None
|
Source code in src/apab/agent/orchestrator.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
start_session ¶
start_session(user_request)
Begin a new agent session and return a :class:RunContext.
Source code in src/apab/agent/orchestrator.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
step ¶
step()
Execute one LLM turn and return the raw response dict.
Source code in src/apab/agent/orchestrator.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
execute_tool_calls ¶
execute_tool_calls(response)
Execute all tool calls from a response and append results to messages.
Source code in src/apab/agent/orchestrator.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
run_to_completion ¶
run_to_completion(user_request, max_turns=20, on_event=None)
Run the full agentic loop and return the final text response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_request
|
str
|
The user's natural-language request. |
required |
max_turns
|
int
|
Maximum number of LLM turns before forcibly stopping. |
20
|
on_event
|
OnEvent | None
|
Optional callback invoked with |
None
|
Source code in src/apab/agent/orchestrator.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
apab.agent.tool_dispatch ¶
Tool dispatch: extract MCP tool schemas and execute tool calls.
ToolDispatcher ¶
Bridges the LLM's tool calls to the MCP server's tool implementations.
Source code in src/apab/agent/tool_dispatch.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
get_tool_schemas ¶
get_tool_schemas()
Extract tool schemas from the MCP server in LLM-friendly format.
Source code in src/apab/agent/tool_dispatch.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
dispatch ¶
dispatch(tool_name, arguments)
Execute a tool call and return the JSON result string.
Uses asyncio to call the async tool function from a sync context.
Source code in src/apab/agent/tool_dispatch.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
save_audit_log ¶
save_audit_log(path)
Persist the audit log to a JSON file.
Source code in src/apab/agent/tool_dispatch.py
115 116 117 118 119 120 121 | |
apab.agent.provider_registry ¶
LLM provider protocol and registry.
ProviderUsage
dataclass
¶
Token and cost tracking for a single LLM call.
Providers expose the most recent call's usage via a last_usage
property. Local providers (e.g. Ollama) report a zero cost estimate.
Source code in src/apab/providers/usage.py
8 9 10 11 12 13 14 15 16 17 18 19 | |
LLMProvider ¶
Bases: Protocol
Protocol for LLM provider backends.
Providers may additionally expose an optional last_usage property
returning an :class:apab.providers.usage.ProviderUsage (or None)
for the most recent chat() call. It is not part of the protocol so
that third-party providers registered via entry points keep working;
consumers must read it with getattr(provider, "last_usage", None).
Source code in src/apab/agent/provider_registry.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
supports_tool_calling ¶
supports_tool_calling()
Whether this provider supports tool-calling.
Source code in src/apab/agent/provider_registry.py
39 40 41 | |
supports_streaming ¶
supports_streaming()
Whether this provider supports streaming responses.
Source code in src/apab/agent/provider_registry.py
43 44 45 | |
chat ¶
chat(messages, tools=None, **kwargs)
Send a chat request and return a normalised response.
The returned dict has at minimum:
- "role" — always "assistant"
- "content" — text content (may be None if tool calls)
- "tool_calls" — list of {"name": str, "arguments": dict}
dicts, or None if no tool calls.
Source code in src/apab/agent/provider_registry.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
discover_providers ¶
discover_providers()
Discover LLM provider classes registered via entry points.
Source code in src/apab/agent/provider_registry.py
64 65 66 67 68 69 70 71 72 73 74 75 76 | |
get_provider ¶
get_provider(provider_name, **kwargs)
Instantiate and return an LLM provider by name.
Source code in src/apab/agent/provider_registry.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
validate_provider ¶
validate_provider(provider_name)
Check if a provider name is available without instantiating it.
Returns True if the provider can be found; logs a warning and
returns False otherwise.
Source code in src/apab/agent/provider_registry.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |