Skip to content
AISpendbook

Your app's data

Vercel AI SDK, LangChain & agents

Send OpenTelemetry spans from the Vercel AI SDK, LangChain, LangGraph, LlamaIndex, OpenAI Agents SDK, Claude Agent SDK and Pydantic AI.

Frameworks that emit OpenTelemetry spans can send usage without a language SDK. Point the exporter at https://www.aispendbook.com/api/v1/ingest/otlp/v1/traces with Authorization: Bearer afk_… (an ingestion key). How each span field is read is on the OpenTelemetry traces page. The cost is an estimate from telemetry, kept apart from billed provider cost.

Vercel AI SDK

The receiver reads the AI SDK's ai.* attributes: model id and provider, prompt and completion tokens (or input, output, cached input and reasoning tokens), embedding tokens, response id, ai.telemetry.functionId as the feature and ai.telemetry.metadata.* for customer and the other fields. Only the inner doGenerate, doStream and doEmbed spans are counted; the outer spans repeat their totals.

Vercel AI SDK
await generateText({
  model,
  prompt,
  experimental_telemetry: {
    isEnabled: true,
    functionId: "support-reply",
    metadata: { customerId: "cus_42", feature: "support" },
  },
});

LangChain, LangGraph and LlamaIndex

Use the OpenInference or OpenLLMetry instrumentation. When no feature is sent, the outermost chain, workflow or agent span names it, and its span id becomes the run id.

LangChain via OpenInference
from openinference.instrumentation.langchain import LangChainInstrumentor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor

provider = TracerProvider()
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter(
    endpoint="https://<api host>/api/v1/ingest/otlp/v1/traces",
    headers={"Authorization": "Bearer afk_..."},
)))
LangChainInstrumentor().instrument(tracer_provider=provider)
# LlamaIndex: openinference.instrumentation.llama_index.LlamaIndexInstrumentor, same exporter

Agent SDKs

  • OpenAI Agents SDK: through the OpenInference instrumentation, with the same exporter.
  • Claude Agent SDK and Claude Code: their beta traces (claude_code.llm_request spans) are read. Turn them on with the environment below.
  • Pydantic AI: model request spans are counted; agent run and tool spans are not.
Claude Code and Claude Agent SDK
export CLAUDE_CODE_ENABLE_TELEMETRY=1
export CLAUDE_CODE_ENHANCED_TELEMETRY_BETA=1
export OTEL_TRACES_EXPORTER=otlp
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://<api host>/api/v1/ingest/otlp/v1/traces
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer afk_..."

Customer and feature on every span

  • Attributes are read from the span, then parent spans in the same export, then the resource.
  • W3C baggage: install the OpenTelemetry BaggageSpanProcessor so entries such as customer_id and feature become span attributes.
  • Settings, API keys, OTLP attribute mapping maps an attribute you already send to customer, feature, environment, end user, run or session. Mapped names are read first.
  • Exporters send child spans before their parents, so a parent in a later export is not seen. Put customer and feature on the spans, the resource or baggage.

Go, Java and the Collector

  • Recipes in github.com/AISpendbook/examples: otel-go (Go with otlptracehttp) and otel-java (Java with Gradle; the README shows Kotlin). Both read SPENDBOOK_ENDPOINT and SPENDBOOK_API_KEY, send one chat span and flush on exit. The Java recipe was run against a local capture server; the Go recipe has not been compiled in CI.
  • One OpenTelemetry Collector can send the same traces to AI Spendbook and to Datadog or Honeycomb:
Collector fan-out
exporters:
  otlphttp/spendbook:
    traces_endpoint: https://<api host>/api/v1/ingest/otlp/v1/traces
    headers:
      Authorization: Bearer ${env:SPENDBOOK_INGESTION_KEY}
  datadog:
    api:
      key: ${env:DD_API_KEY}
  otlp/honeycomb:
    endpoint: api.honeycomb.io:443
    headers:
      x-honeycomb-team: ${env:HONEYCOMB_API_KEY}
service:
  pipelines:
    traces:
      receivers: [otlp]
      processors: [batch]
      exporters: [otlphttp/spendbook, datadog, otlp/honeycomb]

Setup for a Collector is on OpenTelemetry Collector. The recipes above are tested with recorded span fixtures, not against live framework runs.