Agent, Runtime & Events¶
An Agent runs one conversation turn at a time and emits typed events.
The Runtime wires configuration, providers, tools, and platforms
together; RuntimeBuilder is the composition entry point used by both
the CLI and the SDK.
Lifecycle¶
Agent supports async with and explicit await agent.aclose(). The
SDK helpers (run_once, run_stream, run_events) close the agent for
you; when you drive Agent directly, close it yourself:
import asyncio
from dashscope_agenticx.sdk import create_agent
async def main():
async with create_agent(session_id="demo") as agent:
async for chunk in agent.run_stream("Hello!"):
print(chunk, end="", flush=True)
asyncio.run(main())
Both run_events() and run_stream() are single-pass async generators.
To stop early, close the generator (contextlib.aclosing) — see
Embedding the SDK.
Event types¶
Every event is an AgentEventEnvelope with a type from AgentEventType:
| Type | Meaning |
|---|---|
run.started |
A turn began |
message.delta |
Incremental assistant text |
reasoning.delta |
Incremental reasoning/thinking text |
plan.updated |
The agent's plan changed |
tool.started / tool.finished |
Tool execution boundaries |
permission.requested / permission.resolved |
Approval flow |
usage.updated |
Token usage accounting |
artifact.created |
A deliverable file was produced |
compression.completed |
Context compression ran |
provider.switched |
Fallback/routing changed provider |
sandbox.status |
Sandbox backend state |
turn.finished |
The turn completed |
error |
A recoverable or fatal error surfaced |
Errors are delivered as error events inside the stream — AgenticX has
no unified exception hierarchy for model/tool failures. See
Eval for the few control-flow exceptions.
Agent¶
Agent
¶
Agent(
provider: LLMProvider,
executor: Executor,
max_turns: int = 50,
memory: MemoryProvider | None = None,
user_name: str = "",
provider_name: str = "",
model_name: str = "",
session_path: Path | None = None,
disabled_caps_provider=None,
directives_provider=None,
system_prompt: str | None = None,
allow_delegate: bool = True,
allowed_tools: list[str] | None = None,
hook_bus: HookBus | None = None,
memory_manager: MemoryManager | None = None,
tool_registry: ToolRegistry | None = None,
skill_manager=None,
session_manager=None,
runtime_config=None,
runtime_id: str = "",
agent_id: str = "",
capability_tools: dict[str, set[str]] | None = None,
json_mode: bool = False,
oneshot: bool = False,
)
Source code in src/dashscope_agenticx/agent.py
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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 | |
capability_tools
instance-attribute
¶
capability_tools = (
capability_tools if capability_tools is not None else {}
)
readonly_hard_cap
instance-attribute
¶
readonly_hard_cap = int(
os.environ.get("AGENTICX_READONLY_HARD_CAP", "40")
)
turn_retry_max
instance-attribute
¶
turn_retry_max = int(
os.environ.get("AGENTICX_TURN_RETRY_MAX", "2")
)
converge_soft_ratio
instance-attribute
¶
converge_soft_ratio = float(
os.environ.get("AGENTICX_CONVERGE_SOFT", "0.6")
)
converge_hard_ratio
instance-attribute
¶
converge_hard_ratio = float(
os.environ.get("AGENTICX_CONVERGE_HARD", "0.85")
)
time_budget_sec
instance-attribute
¶
time_budget_sec = float(
os.environ.get("AGENTICX_TIME_BUDGET_SEC", "0")
)
time_soft_ratio
instance-attribute
¶
time_soft_ratio = float(
os.environ.get("AGENTICX_TIME_SOFT", "0.5")
)
time_hard_ratio
instance-attribute
¶
time_hard_ratio = float(
os.environ.get("AGENTICX_TIME_HARD", "0.8")
)
silent_tool_nudge
instance-attribute
¶
silent_tool_nudge = int(
os.environ.get("AGENTICX_SILENT_TOOL_NUDGE", "6")
)
experience_tracker
instance-attribute
¶
experience_tracker = (
self.memory_manager.persistent.experience
)
note_skill_use
¶
note_skill_use(name: str) -> None
Queue an explicit /skill invocation for turn/session stats.
Source code in src/dashscope_agenticx/agent.py
527 528 529 | |
reset
¶
reset()
Clear in-memory messages without deleting the persisted history file.
Source code in src/dashscope_agenticx/agent.py
541 542 543 544 | |
load_session
¶
load_session() -> int
Restore self.messages. Returns the number of messages loaded (0 if nothing could be restored).
The per-topic event stream is the source of truth: the latest
messages/snapshot wins when present (crash recovery via
append-only replay; torn trailing lines are skipped). The
history.json file remains as the compat fallback for
sessions written before snapshots existed.
Source code in src/dashscope_agenticx/agent.py
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 | |
save_session
¶
save_session() -> None
Persist self.messages atomically. No-op when session_path is unset.
Source code in src/dashscope_agenticx/agent.py
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 | |
update_system_prompt
¶
update_system_prompt(system_prompt: str | None) -> None
Refresh instructions without discarding conversation state.
Source code in src/dashscope_agenticx/agent.py
650 651 652 653 654 655 656 657 658 | |
aclose
async
¶
aclose() -> None
Release the owning Runtime, or this standalone Agent's provider.
Source code in src/dashscope_agenticx/agent.py
983 984 985 986 987 988 989 990 991 992 993 994 | |
run_events
async
¶
run_events(user_input) -> AsyncGenerator[AgentEvent, None]
Run one turn as a typed event stream.
This is the canonical public execution API. run_stream is a text
compatibility projection over the message-delta events emitted here.
Source code in src/dashscope_agenticx/agent.py
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 | |
run_stream
async
¶
run_stream(user_input) -> AsyncGenerator[str, None]
Yield the legacy text projection of the typed event stream.
Source code in src/dashscope_agenticx/agent.py
1085 1086 1087 1088 1089 1090 | |
Runtime¶
Runtime
dataclass
¶
Runtime(
config: Config,
provider: LLMProvider,
executor: object,
context: RuntimeContext,
memory: MemoryProvider | None = None,
)
A configured provider/executor plus isolated runtime state.
track_provider
¶
track_provider(provider) -> None
Source code in src/dashscope_agenticx/runtime.py
125 126 127 128 129 | |
activate
¶
activate(*, tool_registry=None)
Bind runtime-owned state for frontend commands and tool calls.
Source code in src/dashscope_agenticx/runtime.py
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 | |
aclose
async
¶
aclose() -> None
Release only this runtime's MCP connections and provider clients.
Source code in src/dashscope_agenticx/runtime.py
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 | |
create_agent
¶
create_agent(
system_prompt: str,
session_id: str | None = None,
*,
oneshot: bool = False
) -> Agent
Construct and fully wire an Agent owned by this Runtime.
Source code in src/dashscope_agenticx/runtime.py
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 | |
RuntimeBuilder
¶
RuntimeBuilder(
config: Config, *, extensions_prepared: bool = False
)
Build a Runtime without leaking mutable registries across instances.
Source code in src/dashscope_agenticx/runtime.py
277 278 279 280 281 282 283 284 | |
build
¶
build() -> Runtime
Prepare extensions, provider, executor, memory, and context.
Source code in src/dashscope_agenticx/runtime.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
RuntimeContext
dataclass
¶
RuntimeContext(
runtime_id: str = (lambda: uuid.uuid4().hex)(),
tool_registry: ToolRegistry | None = None,
skill_manager: SkillManager | None = None,
hook_bus: HookBus | None = None,
session_manager: SessionManager | None = None,
capability_tools: dict[str, set[str]] = dict(),
mcp_clients: dict = dict(),
permission_policy: object | None = None,
extensions: object | None = None,
)
Instance-local mutable registries owned by one Runtime.
runtime_id
class-attribute
instance-attribute
¶
runtime_id: str = field(
default_factory=lambda: uuid.uuid4().hex
)
capability_tools
class-attribute
instance-attribute
¶
capability_tools: dict[str, set[str]] = field(
default_factory=dict
)
create
classmethod
¶
create(config: Config) -> 'RuntimeContext'
Create an isolated context from the imported built-in baseline.
Source code in src/dashscope_agenticx/runtime.py
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 | |
import_builtin_tools
¶
import_builtin_tools() -> None
Populate the default registry with all built-in static tools.
Source code in src/dashscope_agenticx/runtime.py
33 34 35 36 37 38 39 40 41 42 | |
create_runtime_memory
¶
create_runtime_memory(
config: Config,
) -> MemoryProvider | None
Create and publish the configured memory provider, if enabled.
Source code in src/dashscope_agenticx/runtime.py
54 55 56 57 58 59 60 | |
Events¶
AgentEventType
¶
Bases: str, Enum
Stable event names for the public runtime stream.
COMPRESSION_COMPLETED
class-attribute
instance-attribute
¶
COMPRESSION_COMPLETED = 'compression.completed'
PERMISSION_REQUESTED
class-attribute
instance-attribute
¶
PERMISSION_REQUESTED = 'permission.requested'
PERMISSION_RESOLVED
class-attribute
instance-attribute
¶
PERMISSION_RESOLVED = 'permission.resolved'
AgentEventEnvelope
dataclass
¶
AgentEventEnvelope(
type: AgentEventType,
payload: dict[str, Any] = dict(),
schema_version: str = "1.0",
runtime_id: str = "",
agent_id: str = "",
session_id: str = "",
run_id: str = "",
turn_id: str = "",
sequence: int = 0,
event_id: str = (lambda: uuid.uuid4().hex)(),
parent_event_id: str = "",
timestamp: str = (
lambda: datetime.now(timezone.utc).isoformat()
)(),
)
Versioned immutable envelope emitted by an Agent run.
ToolDisplayText
¶
iter_message_deltas
async
¶
iter_message_deltas(
agent, user_input
) -> AsyncIterator[str]
Yield display text from the typed API with legacy-agent fallback.
Source code in src/dashscope_agenticx/events.py
92 93 94 95 96 97 98 99 100 101 102 | |