Skip to content

Conversation

@dancer
Copy link
Collaborator

@dancer dancer commented Jul 7, 2025

Background

Right now there doesn't appear to be a way for tracing providers to get accurate metadata if users are using Anthropic/OpenAI/Gemini's prompt caching features. The tokens used to create/read caches are returned in a providerMetadata field on the response that does not seem to be passed anywhere into the exported spans.

Summary

Adds a new ai.response.providerMetadata span attribute that exposes this response field.

Verification

Ran pnpm build and modified the anthropic-cache-control.ts example. Saw the new span attribute present:

{
  resource: {
    attributes: {
      'service.name': 'unknown_service:/usr/local/bin/node',
      'telemetry.sdk.language': 'nodejs',
      'telemetry.sdk.name': 'opentelemetry',
      'telemetry.sdk.version': '2.0.1'
    }
  },
  instrumentationScope: { name: 'ai', version: undefined, schemaUrl: undefined },
  ...
  attributes: {
    'operation.name': 'ai.generateText',
    'ai.operationId': 'ai.generateText',
    'ai.model.provider': 'anthropic.messages',
    'ai.model.id': 'claude-3-5-sonnet-20240620',
    'ai.settings.maxRetries': 2,
    'ai.prompt': `...`,
    'ai.response.providerMetadata': '{"anthropic":{"cacheCreationInputTokens":2157}}',
    'ai.usage.promptTokens': 10,
    'ai.usage.completionTokens': 456
  },
  ...
}

Tasks

  • Tests have been added / updated (for bug fixes / features)
  • Documentation has been added / updated (for bug fixes / features)
  • A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root)
  • Formatting issues have been fixed (run pnpm prettier-fix in the project root)

Related Issues

Fixes #7079

@dancer dancer merged commit a288694 into v4 Jul 7, 2025
8 checks passed
@dancer dancer deleted the js/otel branch July 7, 2025 16:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants