Observability for AI Agents at Scale: Latency, Cost, and Failure Attribution

    As AI agents move from validation into daily workflows, the questions that matter shift from "did this run work?" to latency attribution, cost attribution, and failure attribution across many tasks, machines, and days. This article explains why local, single-session, real-time data is not enough at scale, how a microkernel plus fully plugin-based ReAct executor produces event streams, and how to rebuild a structured four-layer call chain (task, turn/step, model call, tool call) on top of those events. It also covers non-invasive plugin-based collection, three practical query patterns, and alerting plus long-term retention.

    1. Observability Problems Only Begin at Scale

    AI agents are now part of everyday development workflows for many teams. During validation, the only question is whether a given task was completed correctly. Once concurrent tasks, user counts, and connected machines all grow at the same time, the people consuming these agents quickly narrow their focus to three very practical questions:

    1. Latency attribution: within the total duration of a task, how much goes to model inference, how much to tool execution, and how much to queuing and scheduling.
    2. Cost attribution: which sessions, models, and task types concentrate token consumption, and whether there are abnormally amplified long-tail sessions.
    3. Failure attribution: whether failures and interruptions happen during planning, tool calls, or result parsing, and whether the process can be fully traced and reproduced.

    These three problem classes share one trait: none of them is a “single record” problem. They are statistical and correlation problems spanning a batch of tasks, a period of time, and multiple machines. If process data stays at the granularity of a single machine, a single session, and real time, an observability gap is inevitable once things scale.

    2. How These Systems Typically Run: Microkernel, Full Plugin Architecture, and a ReAct Loop

    To understand the blind spots, you first need to understand how this class of agentic coding tool (a command-line agent executor) actually runs. Mainstream implementations in the industry generally share three characteristics.

    First, a microkernel with a fully plugin-based organization. The kernel only assembles plugins according to a profile and manages their lifecycle; model adapters, tool sets, sandbox policies, session persistence, and other capabilities all exist as plugins. The command-line interface, the terminal interface, and the headless form all share the same kernel. One direct consequence of this design: observability must also be built as a plugin and attached to the same assembly chain, so that it stays non-invasive, pluggable, and trimmable.

    Second, a ReAct-style execution model. One user task is called a turn, a turn contains several steps, and each step consists of “reason, call a tool, observe the result.” The key issue: the number of loop iterations and which tools get called are decided by the model at runtime, so the execution structure cannot be known in advance. This means the traditional trace model that depends on a fixed call topology cannot be applied directly; the system must support a dynamically expanding number of steps under the same turn and arbitrarily branching tool calls.

    Third, two kinds of data produced naturally. One is an in-browser session trace view that organizes execution records by turn and shows token usage and latency for individual records. The other is a complete event stream written to a local session directory as compressed JSONL, with no data loss, plus full-text search over tool call arguments and return results.

    3. Why Having Data on Disk Is Still Not Enough

    The scope of the capabilities above is very clear: local machine, single session, real time. They answer “I am debugging this one task right now,” not “how did all tasks perform over the past seven days.” As the number of machines and the concurrency level rise, three gaps widen quickly:

    • Gap 1: no call relationships. Process data is a time-ordered event sequence with no explicit parent-child links, so it cannot answer which step, which turn, or which task a given tool call belongs to.
    • Gap 2: no time-occupancy statistics. The event stream records the order of events but does not break latency down into inference, tool execution, waiting, and retries, which makes the real bottleneck hard to locate.
    • Gap 3: data stays local. Once a machine is reclaimed or a container is destroyed, the historical scene disappears, and cross-machine aggregation or long-horizon retrospection becomes impossible.

    4. Target Data Model: Reconstructing the Event Stream as a Structured Call Chain

    The core of closing these gaps is rebuilding call relationships on top of the native event stream, forming four aggregatable layers:

    Layer Meaning Key fields (illustrative)
    Task The full lifecycle of one user request task_id, status, total duration, total tokens
    Turn/Step Each round of reasoning and observation within a turn step_index, model name, per-stage latency
    Model call Each request-response pair prompt/completion tokens, time to first token
    Tool call Each tool execution tool name, argument digest, return code, retry count

    The illustrative relationship logic can be expressed like this:

    1. task_id
    2. └── step[0..n]
    3. ├── model_call{ tokens_in, tokens_out, latency }
    4. └── tool_call{ name, args_digest, status, duration }

    With this model, “was the time spent on inference or on tools” becomes a single grouped aggregation; “which sessions and models concentrate the tokens” becomes a sum over the task_id and model dimensions; and “which step did the failure happen in” becomes filtering by status and then walking back through the step sequence.

    5. Collection Implementation: Mounting as a Native Plugin

    The most reliable engineering approach is to mount a plugin into the kernel assembly flow, subscribe to events during execution, enrich them with relationship fields as they are produced, and report them in batches, rather than parsing log files after the fact. There are four key points:

    1. Non-invasive: no changes to business code or tool implementations, only event subscription.
    2. Asynchronous and batched: a local in-memory buffer plus batch sending, so reporting never blocks the main execution path.
    3. Degradable: when reporting fails, data is retained locally in a bounded cache and backfilled after recovery, which guarantees that critical context is not lost.
    4. Minimized sensitive data: arguments and return values are digested and redacted, keeping only the length and status information needed to locate problems.

    6. From Data to Conclusions: Three Query Patterns in Practice

    • Latency attribution: aggregate total duration per task, then sum by segment within each step to output the inference share, tool share, and other share. If the tool share is abnormally high, investigate external dependency latency and retry policies first.
    • Cost attribution: cross-tabulate tokens across three dimensions (session, model, task type) to identify long-tail sessions; handle oversized contexts and repeated-read tool calls as a dedicated effort.
    • Failure retrospection: start from the failed status, locate the owning step and tool upward, and restore the full argument digest and return code, so that “a failure from several days ago” becomes reproducible.

    7. Alerting and Long-Term Retention

    Once trace data is ingested, it can be connected to general-purpose logging services, object storage, and monitoring and alerting capabilities: raw events are archived long term for audit and retrospection, aggregated metrics go into dashboards for trend observation, and threshold alerts are set for failure rate, per-task latency, and token growth. In these scenarios, Baidu AI Cloud typically provides combinations of general-purpose capabilities, and teams can freely choose the implementation form that fits their own tech stack.

    8. Conclusion

    Observability at scale is essentially upgrading process data from “local machine, single session, real time” to “cross-session, cross-machine, long-term retained” structured traces. First define the target data model, then collect non-invasively through a plugin, and finally turn data into conclusions with aggregation and alerting. This path does not depend on any specific product and applies to all agent frameworks built on a ReAct loop.