Observability for AI Agents at Scale: From Local Event Streams to Structured Traces

    As coding agents move into daily development workflows, teams stop asking whether a single run was correct and start asking where the time went, where the tokens went, and where failures happened. This article explains why built-in, single-machine observability cannot answer those questions, how a microkernel and fully plugin-based agent architecture with a ReAct loop makes collection possible without touching business logic, and how to reconstruct execution as structured spans across turns, steps, model calls, and tool calls. It covers query patterns for latency attribution, cost analysis, and failure replay, plus practical guidance on low-intrusion collection, sampling, redaction, tiered retention, and trace-based alerting.

    1. Once Agents Scale, the Questions Change

    Coding agents are now part of the daily development workflow for many teams. At scale, the people consuming them no longer care only about whether a single result was correct. They care about three things:

    • Latency attribution: Is the time spent in model inference or in tool execution?
    • Cost distribution: Which sessions and which models concentrate token consumption?
    • Failure replay: At which step did a failure or interruption occur, and can the process be reconstructed?

    These three questions share one trait: none of them is a single-session problem. They are statistical and retrieval problems that span multiple sessions, multiple machines, and multiple points in time. Yet the observability built into most coding tools is scoped to exactly the opposite: local machine, single session, real time.

    2. Typical Architecture: Microkernel, Fully Plugin-Based, ReAct Loop

    Understanding the observability gap starts with understanding how these tools execute. Mainstream coding agent frameworks fall into roughly two design camps; this article discusses the “microkernel plus fully plugin-based” camp.

    The kernel only assembles plugins according to a profile and manages their lifecycle. Model adapters, tool sets, sandbox policies, and session persistence are all pluggable plugins. The CLI, terminal interaction, and headless form factors share the same kernel. Observability is mounted at this layer too, and that is the key premise: collection does not require intrusion into business logic.

    The execution model is a ReAct loop. One user task is a turn, and each round of “reason, call a tool, observe the result” inside a turn is a step. The number of rounds and which tools get called are decided by the model at runtime, so the execution structure cannot be known before the run starts.

    This leads to two consequences. First, the call graph is not a fixed topology but a dynamic tree or mesh. Second, static instrumentation cannot cover it; the structure can only be reconstructed from an event stream.

    3. Out-of-the-Box Local Observability, and Its Limits

    Built on the event stream and on-disk data, these tools typically offer three capabilities:

    1. Session trajectory view: A browser-based execution record organized by round, where a single record shows token usage and duration.
    2. Session event stream persisted to disk: The complete event stream is written to the local session directory as compressed JSONL, with no data loss.
    3. Tool call retrieval: Tool call arguments and return results are recorded in the event stream and support full-text search across sessions.

    This works well for “single machine, single session, real-time debugging.” But it is a time-ordered sequence of events with no call relationships and no per-node duration breakdown. Three problems surface immediately at scale:

    • A single-machine view cannot answer global questions: Once there are many machines, questions like “overall P95 latency last week” cannot be computed from local data.
    • No parent-child relationship, no latency drill-down: You see a list of events, but not which tool call blocked an entire turn.
    • Data lifecycle is tied to the machine: After a machine is reclaimed or a disk is cleaned, a failure scene from a few days ago can no longer be reproduced.

    4. The General Approach: Reconstruct Execution as a Structured Call Chain

    The fix is to add an agent observability collection and aggregation layer, mounted as a native plugin at the kernel plugin layer, that reconstructs execution into a structured call chain covering four entity types:

    Entity Meaning Key attributes
    Task / reasoning round Turn and step Round index, status, start and end time
    Model call One inference request Model identifier, input/output tokens, time to first token
    Tool call One tool execution Tool name, argument summary, return status, duration
    Session association Session and task ownership Session ID, user/project tags

    A sketch of the span structure (pseudocode, field semantics only):

    1. {
    2. "trace_id": "session-uuid",
    3. "span_id": "step-3-tool-1",
    4. "parent_span_id": "step-3",
    5. "name": "tool.shell.exec",
    6. "start": 1730000000123,
    7. "duration_ms": 842,
    8. "attributes": {
    9. "agent.turn": 1,
    10. "agent.step": 3,
    11. "tool.result.status": "error",
    12. "model.tokens.output": 0
    13. }
    14. }

    With parent-child relationships and a unified timeline, the three earlier questions finally have a basis for computation: latency drill-down becomes aggregation by parent_span_id, cost distribution becomes a group by on the model dimension, and failure replay becomes fetching the full chain by trace_id.

    5. Query Patterns for Three Core Scenarios

    Latency attribution. On the call chain, separate spans into “inference time” and “tool time,” aggregate by turn, and compute the share of each. A common finding is that tool execution (especially builds, tests, and network requests) takes the majority, in which case the optimization target is tool-side timeouts and concurrency rather than swapping models.

    Cost analysis. Use the model call span as the unit of granularity and aggregate input/output tokens across three dimensions: session, model, and project tag. Distinguish “tokens that produced useful output” from “tokens consumed by retries and failures”; the latter is often the real source of a cost anomaly.

    Failure replay. Take the full chain by trace, locate the first span with status=error, and check whether subsequent steps entered a futile retry loop. Because the event stream itself is lossless, chain data can be cross-checked against the raw local events.

    6. Implementation Notes and Common Pitfalls

    • Collection must be low-intrusion: Mounting as a plugin without modifying business code is what allows all three runtime form factors to be covered.
    • Sampling and full capture must coexist: Full chains for failure review, sampled chains for long-term trends, with the strategy differentiated by environment.
    • Redact sensitive data: Tool arguments and returns may contain keys or internal addresses, so apply field-level redaction and summarization before writing to storage.
    • Tier data retention: Hot data for real-time retrieval, cold data moved to object storage for long-term archival, closing the loop with logging services and monitoring alerts.
    • Alert on the chain: Rules such as “step count per turn exceeds threshold,” “sudden spike in tool error rate,” and “tokens per session exceed budget” sit closer to real incidents than isolated single-point metrics.

    7. Conclusion

    Observability for agents is essentially about turning “a dynamic execution structure that is only determined at runtime” into “structured data that can be aggregated, retrieved, and retained long term.” Local event streams solve visibility; call chains solve the ability to compute, to look things up, and to keep them. Baidu AI Cloud likewise emphasizes standardized chain data to support cost, performance, and stability needs in the agent observability space. Teams building their own solution should start with three foundational capabilities: plugin-based collection, unified span semantics, and tiered retention.