You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(ai): Expose provider metadata as an attribute on exported OTEL spans (#7096)
## 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
- [x] Tests have been added / updated (for bug fixes / features)
- [x] Documentation has been added / updated (for bug fixes / features)
- [x] A _patch_ changeset for relevant packages has been added (for bug
fixes / features - run `pnpm changeset` in the project root)
- [x] Formatting issues have been fixed (run `pnpm prettier-fix` in the
project root)
## Related Issues
Fixes#7079
0 commit comments