Kaikki artikkelit
25. marraskuuta 2026

AI agent tool call logging: what to record, how to replay and how to review failures

Log every AI agent tool call as one structured record, keep both failure channels, redact arguments, replay recorded results and review by failure class.

Abstract flow of connected boxes and arrows with one acid step on a dotted canvas
Illustration: Kallos Labs.

To evaluate an AI agent's tool calls, write one structured record per call, keep protocol errors and tool execution errors as separate failure types, redact arguments before they reach storage, replay recorded results instead of live systems, and review failures by class on a fixed schedule. One caveat on timing: as of protocol version 2026-07-28, the Model Context Protocol (MCP) deprecates logging over the protocol itself, so the log has to live in your own infrastructure.

Before you start

Three things come first: a clear idea of what a tool call is, a place for logs to go, and a rule for who may read them.

A tool call is a request from the agent's host to a tool provider. In MCP, a client discovers tools with tools/list and executes them with tools/call, as the MCP architecture overview describes. Each call is a JSON-RPC 2.0 message with an id that ties the request to its response. That id is the natural seed for your own call identifier. If you want the background first, read what MCP is and how agents use it.

Next, decide where logs go. The MCP debugging guide says to use structured logs to stderr for stdio servers, or OpenTelemetry for all transports. It also warns that local servers "should not log messages to stdout (standard out), as this will interfere with protocol operation." For servers on the Streamable HTTP transport, it notes that stderr is not captured by the client, so you need your own log aggregation.

Finally, set access. A tool call log holds what users asked for and what systems returned. Treat it like the audit logs, permissions and approval gates that already protect the agent, and give it the same retention and read rules.

Step-by-step

Step 1: Write one record per call

Checklist diagram with six items: Wrong tool, Bad arguments, Tool failure, Timeout, Ignored result and Denied
Checklist: Step-by-step.

Emit a single structured record for each tool call, written when the call finishes. A flat JSON object is enough. Include:

  • A run id that groups every call in one agent session.
  • A call id, unique within the run.
  • The tool name and the version of its input schema.
  • A start timestamp and a duration in milliseconds.
  • A status: success, tool error, protocol error, timeout or denied.
  • An error class when the status is not success.

The MCP debugging guide lists the events worth logging: startup steps, resource access, tool execution, error conditions and performance metrics. Its logging strategy asks for consistent formats, context, timestamps and request IDs, plus operation timing and latency. A per-call record covers all of that in one place.

The MCP tools specification also says clients "SHOULD" log tool usage for audit purposes. It lists that next to timeouts and showing tool inputs to the user before the call goes out. Log the timeout you configured as well, so a slow call and a cut-off call look different later.

Log the tool list the agent saw, too. MCP change notifications are, in the architecture page's words, "Best Effort", so a client can act on a stale list. Store a hash of the tool list with the run, and you can tell a bad choice from a missing tool.

Step 2: Keep both failure channels

MCP reports tool problems in two places. The tools specification describes them this way: protocol errors are "standard JSON-RPC errors for issues like unknown tools, invalid arguments, server errors", and tool execution errors are "reported in tool results with isError: true", covering API failures, invalid input data and business logic errors.

The split matters for review. A protocol error usually means the agent or the host is wrong: a tool name that does not exist, or arguments that fail validation. An isError: true result usually means the tool ran and something outside the agent's control went wrong, such as a rate limit. The model sees the second kind in its context and may retry. It may never see the first.

Store the channel as its own field, separate from the error text. A single "failed" status hides the most useful question in a review: was the call malformed, or did the world say no?

Step 3: Redact before you write

Arguments and results are where the risk sits. The debugging guide tells you to sanitize logs, protect credentials and mask personal information. The simplest reliable method is to redact at the point of logging, not at read time.

  • Drop or hash fields you never need to read, such as tokens, email addresses and free-text user content.
  • Keep the shape of the arguments (field names and types) even when values are removed, so you can still see a malformed call.
  • Store full payloads only in a short-lived, restricted store, if at all.

OpenTelemetry takes the same stance. In its generative AI conventions, tool call arguments and results are opt-in attributes with a sensitive-data warning. Make your own default match: off, until a reason exists.

Step 4: Emit a trace span per call

If you already run OpenTelemetry, record each call as a span. The GenAI semantic conventions for the execute tool span name it execute_tool {gen_ai.tool.name} and set the span kind to INTERNAL. Two attributes are required: gen_ai.operation.name (set to execute_tool) and gen_ai.tool.name. error.type is required when the call ended in an error. The call id, tool description and tool type are recommended.

Those conventions carry Development status as of this writing, so attribute names can change between versions. Pin the version you target and wrap the attribute names in one small module, so a rename is a one-file change.

One practical gain: traces show the agent's reasoning step, the tool call and the follow-up model request as one tree. A slow answer then points at either the model or the tool without guesswork.

Step 5: Replay recorded calls

A replay re-runs an agent step using the logged record instead of the live tool. It answers the question "what would the agent do with this exact result?" without touching anything real.

Use two modes:

  1. Recorded replay. Feed the stored result for each call back into the loop and compare the agent's next action. This is safe for every tool, because nothing runs.
  2. Live replay. Call the real tool again. Allow this only for read-only tools, and mark the run as a replay so the call never counts as production traffic.

Never replay a tool that writes, sends, charges or deletes against a live system. If a recorded result is missing or redacted, the replay stops there and says so. Don't let it guess.

Replays are most useful when you turn failures into tests. Take a failed run, keep its recorded results, and add it to the eval set so the same failure is checked on every change to the prompt, model or tool descriptions.

Step 6: Review failures by class

Reading logs one by one doesn't scale. Group failures into a small set of classes and count them. A workable set:

  • Wrong tool: the agent picked a tool that does not fit the task.
  • Bad arguments: the call failed schema validation, a protocol error.
  • Tool failure: the tool returned isError: true.
  • Timeout: the call hit the limit you set.
  • Ignored result: the tool succeeded and the agent acted as if it had not.
  • Denied: a person or policy refused the call.

Run the review on a fixed cadence, weekly for a new agent and less often once the counts settle. Sample successful calls as well as failures. An agent that succeeds by calling the same tool six times is not healthy, and only the success logs show it. Keep a short change log of the fixes you make, tagged by class, so you can see whether a class shrinks.

Common mistakes

  • Logging to stdout on a stdio server. It corrupts the protocol stream. Use stderr.
  • Logging only failures. You lose the baseline for latency, call counts and the successful-but-wasteful runs.
  • Storing raw arguments forever. Redact on write and set a retention limit.
  • One error field for everything. Separate protocol errors, tool errors and timeouts.
  • Replaying writes live. Replay recorded results, and keep live replay to read-only tools.
  • Reviewing once. Failure classes shift whenever tools, prompts or models change.

Teams that want help wiring this into an agent they already run can look at Kallos Labs' AI automation work.

Frequently asked questions

What should an AI agent tool call log contain?

Every record should hold a run id, call id, tool name, timestamp, duration, status and error class. Add arguments and results only after redaction, and store the tool list the agent saw so you can judge its choices.

Should you log full tool arguments and results?

Not by default. OpenTelemetry's GenAI conventions mark arguments and results as opt-in because they may contain sensitive data, so store redacted copies or hashes and keep full payloads in a short-lived, restricted store.

Can you replay a tool call against the live system?

Only for read-only tools. For anything that writes, sends or charges, replay the recorded result so no real action runs, and flag the run as a replay.

How often should you review failed tool calls?

Review every failure class when a new tool ships, then on a fixed cadence such as weekly. Sample successful calls too, since repeated or wasteful calls only show up there.