Data Flow
How a message moves from a channel or CLI call through the agent runtime, tool gateway, model router, and memory — and back out.
5 min read
Purpose
Trace what actually happens between "you send a message" and "the agent replies," for both interactive and detached/background runs. This complements Event Flow (the pub/sub backbone) and Sequence Diagrams (specific call sequences).
The agent runtime state machine
Every agent run — regardless of which channel or entry point triggered it — passes through the same state machine:
idle → assembling_context → calling_model → [tool_executing]* → awaiting_approval → storing_memory → completed | failedassembling_context— persona, skills, soul, model config, and prior session messages are loaded and merged into a prompt.calling_model— the model router (@anvio/models) sends the assembled prompt to the configured provider or vendor runtime.tool_executing(repeatable, zero or more times) — if the model requests a tool call, the tool gateway executes it and the result is fed back to the model, for up to 5 round-trips.awaiting_approval— runs pause here if a tool call requires human approval; resumed viaruntime.resume(session, agent, decision)after anAPPROVAL_DECIDEDevent.storing_memory— the completed exchange is persisted to the configured memory provider.completed|failed— terminal states.
The runtime is channel-agnostic: it never imports channel-specific code. All outbound communication (progress, completion, errors) flows through events to the Channel Hub, which fans out to whichever adapter originated the request.
Interactive chat flow
This is what happens on anvio chat --agent <name> or anvio session 1on1 --agent <name>. Native tool_use is used for Anthropic, OpenAI, and Gemini providers; other providers use fenced ```anvio_tool``` blocks in the model's text output, parsed by the tool gateway.
Detached / background runs
anvio run architect "Refactor the auth module" --detachWhen detached: true, the worker (apps/worker, or the agent-worker role inside the unified gateway) executes the run independently of the client connection that submitted it. Progress and completion are delivered via channel adapters and notifications rather than a blocking response:
- The runtime yields
progressevents during context assembly, model calls, and memory storage. - The worker publishes an
AGENT_RUN_PROGRESSevent, consumable across all channels. - You poll or tail the result with
anvio status <sessionId>,anvio logs <sessionId>, oranvio sessions list.
Mid-run instruction injection
anvio inbox <sessionId> "Focus on error handling"Instructions injected via anvio inbox land in an inbox queue. The worker drains the inbox on AGENT_INBOX_INJECTED and either re-queues the agent run with the new instruction or stops the session, depending on context.
Web Chat / full-stack flow
When Anvio is running behind the gateway (or the split worker + API + gateway stack), a browser-originated message takes this path:
User → Gateway (WS) → API → Event bus → Worker → Runtime → Model → Memory → Event bus → Gateway → UserThe event bus here is either the in-process LocalEventBus (Level 1/2) or NATS JetStream (Level 3+) — see Event Flow for the transport details and Sequence Diagrams for the fully expanded version of this flow.
Storage touched along the way
| Stage | Storage (Level 1 default) | Storage (opt-in) |
|---|---|---|
| Session messages | workspace/sessions/*.json (JSONL) | workspace/state.db (SQLite + FTS5) when storage.provider: sqlite |
| Long-term memory | workspace/memory/ JSON files | SQLite, PostgreSQL, Qdrant, or Honcho via spec.memory.provider |
| OAuth/connection tokens | workspace/connections/ (encrypted) | Same at every level — always encrypted at rest |
| Skill drafts (learning loop) | workspace/skills/_drafts/ | Promoted to workspace/skills/ via anvio learning promote |
Orchestration patterns
The runtime supports more than single-agent execution:
- Single agent — default
runtime.run/runtime.stream. - Supervisor —
SupervisorOrchestratordelegates to subagents. - Parallel / fan-out — concurrent subagent execution.
- Fan-in — a manager agent synthesizes subagent results.
- Hierarchical — nested orchestration plans.
See Subagent delegation for the full design.
Best practices
- Use
anvio session export <id> --mdto get a readable trajectory export of a completed run — useful for debugging exactly what context, tool calls, and model responses occurred. - For long-running or exploratory tasks, prefer
--detachand monitor withanvio status/anvio logsrather than blocking a terminal on an interactive session. - If a run seems stuck at
awaiting_approval, checkanvio approve <session> <requestId>— the state machine will not proceed past that state without an explicit decision.
Common mistakes
- Expecting tool calls to loop indefinitely. The model/tool round-trip is capped at 5 rounds per turn — a task that needs more back-and-forth should be broken into multiple runs or delegated to subagents.
- Assuming detached runs stream to the terminal that started them. They don't — output arrives via channel adapters/notifications, not the original stdin/stdout.
- Forgetting the worker must be running for
--detachto do anything. Withoutapps/worker(oranvio gateway start) running, a detached run is queued but never picked up.
References
Last updated on