# OpenTelemetry traces

Send GenAI spans over OTLP/HTTP, as JSON or protobuf, and see how each span becomes a usage event.

AI Spendbook documentation, Your app's data. Web page: https://www.aispendbook.com/docs/opentelemetry

If your services already emit OpenTelemetry traces, send them to `POST https://www.aispendbook.com/api/v1/ingest/otlp/v1/traces` with an ingestion key (`Authorization: Bearer afk_…`). Spans that carry model usage attributes become usage events and go through the same validation, data policy, de-duplication and pricing as the [ingestion API](https://www.aispendbook.com/docs/ingestion-api).

The receiver is tested with recorded span fixtures from the Vercel AI SDK, LangGraph, LlamaIndex, LangChain, the OpenAI Agents SDK, the Claude Agent SDK, Pydantic AI and Kong AI Gateway. It has not been run against a live exporter or account. Framework setup is on [Vercel AI SDK, LangChain and agents](https://www.aispendbook.com/docs/agent-frameworks).

- OTLP/HTTP is accepted with JSON encoding (`Content-Type: application/json`) or protobuf encoding (`application/x-protobuf`), each with or without `Content-Encoding: gzip`. Any other content type or compression is answered with 415. OTLP over gRPC is not served.
- The request body limit also applies to a gzip body after it is decompressed. A larger body is answered with 413.
- Three attribute vocabularies are read, in this order for each field: the OpenTelemetry GenAI semantic conventions (`gen_ai.*`), the names written by the OpenLLMetry (Traceloop) instrumentations, and the OpenInference conventions (`llm.*`). The Vercel AI SDK's `ai.*` attributes and Claude Code's `claude_code.llm_request` spans are read too.
- A request may contain at most 500 usage spans. The rate limit is the one that applies to the ingestion key.

```bash
curl -sS -X POST "https://www.aispendbook.com/api/v1/ingest/otlp/v1/traces" \
  -H "Authorization: Bearer $SPENDBOOK_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"resourceSpans": [{
    "resource": {"attributes": [{"key": "deployment.environment.name", "value": {"stringValue": "production"}}]},
    "scopeSpans": [{"spans": [{
      "traceId": "5b8efff798038103d269b633813fc60c",
      "spanId": "eee19b7ec3c1b174",
      "startTimeUnixNano": "1795183380000000000",
      "endTimeUnixNano": "1795183381200000000",
      "attributes": [
        {"key": "gen_ai.provider.name", "value": {"stringValue": "anthropic"}},
        {"key": "gen_ai.request.model", "value": {"stringValue": "claude-haiku-5-5"}},
        {"key": "gen_ai.usage.input_tokens", "value": {"intValue": "1200"}},
        {"key": "gen_ai.usage.output_tokens", "value": {"intValue": "300"}}
      ]
    }]}]
  }]}'
```

## How a span is mapped

| Event field | Read from |
| --- | --- |
| event_id | otlp:{traceId}:{spanId}. A span sent again is a duplicate, never a second event. |
| provider | gen_ai.provider.name, else gen_ai.system, llm.provider or llm.system, lowercased |
| model | gen_ai.response.model, else gen_ai.request.model or llm.model_name |
| input_tokens | gen_ai.usage.input_tokens, else gen_ai.usage.prompt_tokens or llm.token_count.prompt, minus the two cache counts below |
| output_tokens | gen_ai.usage.output_tokens, else gen_ai.usage.completion_tokens or llm.token_count.completion |
| cache_read_tokens | gen_ai.usage.cache_read.input_tokens, else gen_ai.usage.cache_read_input_tokens or llm.token_count.prompt_details.cache_read |
| cache_write_tokens | gen_ai.usage.cache_write.input_tokens, else gen_ai.usage.cache_creation.input_tokens, gen_ai.usage.cache_creation_input_tokens or llm.token_count.prompt_details.cache_write |
| other_tokens.reasoning_tokens | gen_ai.usage.reasoning.output_tokens, else gen_ai.usage.reasoning_tokens, llm.usage.reasoning_tokens or llm.token_count.completion_details.reasoning |
| request_id | gen_ai.response.id |
| trace_id | the span's traceId |
| session_id | session.id, else gen_ai.conversation.id, on the span and then on the resource |
| environment | the resource's deployment.environment.name, else deployment.environment |
| customer_id | spendbook.customer_id, else traceloop.association.properties.customer_id |
| feature | spendbook.feature, else traceloop.association.properties.feature |
| end_user_id | spendbook.end_user_id, else user.id |
| category, workload_type, project_id, organization_ref, run_id | spendbook.category, spendbook.workload_type, spendbook.project_id, spendbook.organization_ref, spendbook.run_id |
| occurred_at | startTimeUnixNano. endTimeUnixNano is stored as the completion time. |
| status | error when the span status code is ERROR, with error.type as the error type |

## Cache and reasoning tokens

- An event's input tokens are uncached input. All three vocabularies define their input count as including cache read and cache write tokens, so both cache counts are subtracted from the reported input. A span whose cache counts are larger than its input count is stored with its input as reported.
- Reasoning tokens are part of the reported output count. They are recorded as a separate dimension and are never added to output tokens.
- Audio and image token counts (`gen_ai.usage.input_audio_tokens`, `output_audio_tokens`, `input_image_tokens`, `output_image_tokens`, the dotted `gen_ai.usage.audio.*` and `gen_ai.usage.image.*` forms, and OpenInference's prompt and completion details) are sub-counts of input and output, never added to them. They are priced at the model's audio or image rate where its price sheet has one; otherwise the record is marked a partial estimate.

## Attribution

- Customer, feature, end user and the other attribution fields are read from the span's attributes first and then from the resource's attributes. Set `spendbook.customer_id` and `spendbook.feature` on a span, or on the resource to attribute everything a service sends.
- Attributes on a parent span in the same export, and W3C baggage entries turned into span attributes by the BaggageSpanProcessor, are read as well.
- Settings, API keys, **OTLP attribute mapping** maps an attribute name you already send to customer, feature, environment, end user, run or session. Mapped names are read first.
- For LangChain, LangGraph, LlamaIndex and agent frameworks, when no feature is sent the outermost chain, workflow or agent span names it, and its span id becomes the run id.
- A span with none of these attributes arrives unattributed.

## What is not mapped

- Wrapper spans: `gen_ai.operation.name` of `invoke_agent`, `create_agent`, `invoke_workflow` or `execute_tool`; `openinference.span.kind` of `CHAIN`, `TOOL`, `AGENT`, `RETRIEVER`, `RERANKER`, `GUARDRAIL`, `EVALUATOR` or `PROMPT`; `traceloop.span.kind` of `workflow`, `task`, `agent` or `tool`. The model calls they wrap arrive as their own spans, and counting both would double the usage.
- Cached audio and image token sub-counts, and the one-hour cache write split.
- A parent span that arrives in a later export than its children, so put customer and feature on the spans, the resource or baggage when they must be complete.

## The response

- Spans with no `gen_ai.*` or `llm.*` attribute, and the wrapper spans above, are ignored.
- A usage span with no provider, model, token count, ids or start time, or one that ingestion rejects, is reported in `partialSuccess` with `rejectedSpans` and `errorMessage`. A fully accepted request answers `{}`. A protobuf request is answered with a protobuf `ExportTraceServiceResponse`.
- Accepted, duplicate, ignored and rejected counts are returned in the `X-Spans-Accepted`, `X-Spans-Duplicates`, `X-Spans-Ignored` and `X-Spans-Rejected` response headers.
- A span for a model that has no price sheet is stored without a cost.
