> ## Documentation Index
> Fetch the complete documentation index at: https://docs.honeycomb.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Instrumenting AI Agents

> Track input prompts, responses, token usage, tool calls, and agent invocations using OpenTelemetry.

AI agents may invoke tools, call other agents, or prompt a Generative AI (GenAI) model with multi-step user input.
The distributed and non-deterministic nature of agentic workflows makes monitoring and debugging difficult.

Instrument your AI agents with OpenTelemetry (OTel) GenAI semantic conventions to get full visibility into agent sessions and explore your agents in the [Agent Timeline](https://docs.honeycomb.io/investigate/observe/agent-timeline.md).

* Refer to [Fast AI Feedback Loops with Honeycomb and OpenTelemetry](https://www.honeycomb.io/blog/fast-ai-feedback-loops-honeycomb-opentelemetry) for an example of agent instrumentation using [Pydantic](https://ai.pydantic.dev/logfire/#setting-opentelemetry-sdk-providers).
* Visit the [example storechat](https://github.com/honeycombio/devrel-opentelemetry-demo/tree/main/src/storechat) application repo for further agent instrumentation examples.
* Avoid [sampling your GenAI telemetry](https://docs.honeycomb.io/manage-data-volume/sample/gen-ai.md).

## Enriching your traces with GenAI context

Add the following [OTel GenAI attributes](https://opentelemetry.io/docs/specs/semconv/registry/attributes/gen-ai/) to your agent spans to give Honeycomb the context it needs to group spans into conversations, identify agents, and surface meaningful data in the Agent Timeline.

**gen\_ai.conversation.id** `string` *required*

Unique identifier for the conversation or session. Used to group all traces and spans belonging to the same agent conversation. All spans in a conversation must have a `gen_ai.conversation.id`.

**gen\_ai.agent.name** `string` *required*

Name of the agent emitting the span.

In multi-agent workflows [each agent should have a unique name](#use-unique-names-for-each-agent).

**gen\_ai.operation.name** `string` *required*

Type of agentic operation occurring:

* `chat`
* `create_agent`
* `embeddings`
* `execute_tool`
* `generate_content`
* `invoke_agent`
* `invoke_workflow`
* `retrieval`
* `text_completion`

For span naming conventions, refer to the [Naming generative AI operation spans](#naming-generative-ai-operation-spans) section.

**gen\_ai.usage.input\_tokens** `int`

Number of tokens used in the GenAI input prompt.

**gen\_ai.usage.output\_tokens** `int`

Number of tokens used in the GenAI response.

**gen\_ai.request.model** `string`

Name of the model requested.

**gen\_ai.response.model** `string`

Name of the model that generated the response. This can differ from the requested model.

**gen\_ai.response.finish\_reasons** `string[]`

Why the model stopped generating tokens.

Examples: `["stop"]`, `["tool_calls"]`, `["stop", "length"]`

**gen\_ai.tool.name** `string`

Name of the tool called by the agent.

**gen\_ai.tool.call.id** `string`

Unique identifier for the tool call.

**gen\_ai.tool.call.arguments** `object | json`

Parameters passed to the tool call.

**gen\_ai.tool.call.result** `string`

Result returned by the tool call (if any).

To learn how to handle failed tool calls, refer to the [Recording errors and exceptions](#recording-errors-and-exceptions) section.

### Recording input prompts, completions, and evaluations

Store input prompts, chat history, completions, and evaluations on your agent spans in JSON format using the following GenAI attributes.

If an agent's inputs and outputs are too large, you can store the content externally and reference it in the span instead.
If the content contains sensitive data, you can filter it with your OTel Collector before the spans reach Honeycomb. See [Capturing instructions, inputs, and outputs](https://github.com/open-telemetry/semantic-conventions-genai/blob/main/docs/gen-ai/gen-ai-spans.md#capturing-instructions-inputs-and-outputs) for more details on this process.

**gen\_ai.input.messages** `object | json`

Chat history or input prompts provided to the model.

[GenAI input messages JSON schema](https://github.com/open-telemetry/semantic-conventions-genai/blob/main/model/gen-ai/gen-ai-input-messages.json)

> [!CAUTION]
>
> GenAI prompts or chats may contain PII or other sensitive data.

**gen\_ai.output.messages** `object | json`

Messages returned by the model. Each message represents a specific model response.

[GenAI output messages JSON schema](https://github.com/open-telemetry/semantic-conventions-genai/blob/main/model/gen-ai/gen-ai-output-messages.json)

> [!CAUTION]
>
> GenAI responses may contain PII or other sensitive data.

**gen\_ai.evaluation.result** `string`

Attach `gen_ai.evaluation.result` events to the GenAI operation span to [review evaluations in the GenAI tab](https://docs.honeycomb.io/investigate/analyze/explore-traces.md#gen-ai-tab).

## Keeping agent names unique

The [Agent Timeline](https://docs.honeycomb.io/investigate/observe/agent-timeline.md) groups spans by `gen_ai.agent.name`, so duplicate or missing names make it impossible to distinguish between agents during an investigation.

Each agent should have its own unique `gen_ai.agent.name`.
Sub-agents should use their own distinct name, instead of inheriting the parent agent's name.

> [!NOTE]
>
> If `gen_ai.agent.name` is omitted on a span, it will show up as `"Unknown"` on the Agent Timeline.

## Instrumenting agent-to-agent calls

In multi-agent workflows, correctly attributing spans to the right agent is what makes the Agent Timeline useful.
The calling agent should emit the `invoke_agent` span, not the agent being called.
The called agent then emits its own spans (`chat`, `execute_tool`, and so on) under its own unique `gen_ai.agent.name`, keeping each agent's work distinct and traceable in the timeline.

To learn more about agent invocation spans, visit the OTel documentation:

* [Invoke agent client span (remote invocation)](https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-agent-spans/#invoke-agent-client-span)
* [Invoke agent internal span (same process invocation)](https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-agent-spans/#invoke-agent-internal-span)

## Handling errors and exceptions

Agentic workflows can fail in subtle ways: a tool call that silently drops its result, or an error that surfaces in a child span but never reaches the parent.
Recording errors consistently gives you the full picture when something goes wrong.

Record errors or exceptions following [the OTel specification](https://opentelemetry.io/docs/specs/semconv/general/recording-errors/) and include as many attributes as apply:

* `error.type` / `exception.type`
* `error.message` / `exception.message`
* `error.stacktrace` / `exception.stacktrace`

For tool call failures, propagate the error status to the parent span.

## Naming generative AI operation spans

Generative AI operation spans should follow these naming conventions.
Naming your spans this way ensures the [Agent Timeline](https://docs.honeycomb.io/investigate/observe/agent-timeline.md) can understand the operation type and display it meaningfully.

| Operation | `gen_ai.operation.name` | Span Name Pattern |
| - | - | - |
| Chat | `chat` | `chat {model}` |
| Create GenAI agent | `create_agent` | `create_agent {agent_name}` |
| Tool execution | `execute_tool` | `execute_tool {tool_name}` |
| Agent invocation | `invoke_agent` | `invoke_agent {agent_name}` |
| Embeddings | `embeddings` | `embeddings {model}` |
| RAG retrieval | `retrieval` | `retrieval {data_source}` |
| Multimodal content generation | `generate_content` | `generate_content {model}` |
| Text completions | `text_completion` | `text_completion {model}` |

## Remapping existing telemetry

In some cases, your existing telemetry may not conform to OpenTelemetry semantic conventions.
The [`transform` processor](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/transformprocessor) lets you remap those attributes before they reach Honeycomb.

For example, [Claude Code can emit telemetry](https://code.claude.com/docs/en/monitoring-usage) using the OpenTelemetry protocol.
Claude Code sets many GenAI attributes itself, such as `gen_ai.conversation.id`, `gen_ai.request.model`, `gen_ai.tool.name`, and `gen_ai.tool.call.id`.
It does not set agent names, semantic-convention span names, or token usage, and its spans are still named `claude_code.*`.
Use a Collector `transform` processor to fill those in.

> [!TIP]
>
> Running agents on Claude Managed Agents or the Claude API?
> Those traces already use the GenAI semantic conventions. Refer to [Send Claude Platform Traces to Honeycomb](https://docs.honeycomb.io/send-data/use-cases/claude-platform.md).

The following configuration fills in agent names, span names, and token usage.

```yml theme={}
processors:
  # Rules run in order, and each one sees the span names set by earlier rules.
  # Match on span.name, not on gen_ai.* attributes being absent: Claude Code
  # already sets gen_ai.operation.name on its spans, so a guard like
  # `attributes["gen_ai.operation.name"] == nil` never matches.
  transform:
    error_mode: ignore
    trace_statements:
      # Default agent name for every span. Subagent rules below override it.
      - context: span
        statements:
          - set(attributes["gen_ai.agent.name"], "Claude Code")

      # claude_code.interaction -> invoke_agent Claude Code
      # Claude Code already sets gen_ai.operation.name = "invoke_agent" here.
      - context: span
        conditions:
          - 'span.name == "claude_code.interaction"'
        statements:
          - set(span.name, Concat(["invoke_agent ", attributes["gen_ai.agent.name"]], ""))

      # claude_code.llm_request -> chat {model}, with token usage.
      # Semconv input_tokens includes cache reads and writes; Claude Code's
      # bare input_tokens does not, so add them together.
      - context: span
        conditions:
          - 'span.name == "claude_code.llm_request"'
        statements:
          - set(attributes["gen_ai.operation.name"], "chat")
          - set(span.name, Concat(["chat ", attributes["gen_ai.request.model"]], "")) where IsString(attributes["gen_ai.request.model"])
          - set(attributes["gen_ai.usage.input_tokens"], attributes["input_tokens"]) where IsInt(attributes["input_tokens"])
          - set(attributes["gen_ai.usage.input_tokens"], attributes["input_tokens"] + attributes["cache_read_tokens"] + attributes["cache_creation_tokens"]) where IsInt(attributes["input_tokens"]) and IsInt(attributes["cache_read_tokens"]) and IsInt(attributes["cache_creation_tokens"])
          - set(attributes["gen_ai.usage.output_tokens"], attributes["output_tokens"]) where IsInt(attributes["output_tokens"])
          - set(attributes["gen_ai.usage.cache_read.input_tokens"], attributes["cache_read_tokens"]) where IsInt(attributes["cache_read_tokens"])
          - set(attributes["gen_ai.usage.cache_creation.input_tokens"], attributes["cache_creation_tokens"]) where IsInt(attributes["cache_creation_tokens"])

      # Subagent spawn -> invoke_agent {subagent_type}
      # Only the subagent-spawning tool carries subagent_type, which requires
      # OTEL_LOG_TOOL_DETAILS=1. Claude Code labels this span execute_tool,
      # so override the operation.
      # Entries in a conditions list are OR-ed; use `and` within one string.
      - context: span
        conditions:
          - 'span.name == "claude_code.tool" and IsString(attributes["subagent_type"])'
        statements:
          - set(attributes["gen_ai.agent.name"], attributes["subagent_type"])
          - set(attributes["gen_ai.operation.name"], "invoke_agent")
          - set(span.name, Concat(["invoke_agent ", attributes["subagent_type"]], ""))

      # All other claude_code.tool spans -> execute_tool {tool_name}
      # Spans renamed by the subagent rule no longer match.
      - context: span
        conditions:
          - 'span.name == "claude_code.tool"'
        statements:
          - set(span.name, Concat(["execute_tool ", attributes["gen_ai.tool.name"]], "")) where IsString(attributes["gen_ai.tool.name"])
```
