Repository navigation
fix(go-sdk): emit cache and reasoning token counts under the keys Python and the UI read - #1987
basil-k-aji-dev wants to merge 1 commit into
Conversation
…hon and the UI read
Reviewer's GuideData structuresAdd constants for the standard cache and reasoning token attribute keys while retaining the existing constants for compatibility.
ImplementationEmit both standard and legacy cache token attributes for Anthropic responses in streaming and non-streaming paths.
Emit the standard cache-read and reasoning token attributes alongside the existing OpenAI attributes for non-streaming responses.
TestsVerify the new and legacy cache token attributes on Anthropic streaming and non-streaming spans.
Verify standard cache-read and reasoning token attributes alongside the existing OpenAI attributes.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
PR Summary by QodoEmit Go SDK cache and reasoning counts under UI-readable keys
AI Description
Merge Risk
|
Code Review by Qodo
1. Streamed OpenAI drops cache read counts
|
Linked issuesChecked at commit c483ecc #1984 Bug: Go SDK writes cache and reasoning token counts under attribute names nothing reads · 3 of 3 met
|
There was a problem hiding this comment.
Hey - I've found 2 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="sdk/go/instrumentation/anthropic/instrumentor.go" line_range="204-208" />
<code_context>
semconv.SetIntAttribute(span, semconv.GenAIUsageTotalTokens, resp.Usage.InputTokens+resp.Usage.OutputTokens)
if resp.Usage.CacheCreationInputTokens > 0 {
semconv.SetIntAttribute(span, semconv.GenAIUsagePromptTokensDetailsCacheWrite, resp.Usage.CacheCreationInputTokens)
+ semconv.SetIntAttribute(span, semconv.GenAIUsageCacheCreationInputTokensKey, resp.Usage.CacheCreationInputTokens)
}
if resp.Usage.CacheReadInputTokens > 0 {
semconv.SetIntAttribute(span, semconv.GenAIUsagePromptTokensDetailsCacheRead, resp.Usage.CacheReadInputTokens)
+ semconv.SetIntAttribute(span, semconv.GenAIUsageCacheReadInputTokensKey, resp.Usage.CacheReadInputTokens)
}
}
</code_context>
<issue_to_address>
**Canonical cache counts remain missing**
When the Anthropic or OpenAI response includes cache-token usage, the new emitters write cache counts under `gen_ai.usage.cache_read.input_tokens` and `gen_ai.usage.cache_creation.input_tokens` rather than the convention’s `gen_ai.usage.cache.read_input_tokens` and `gen_ai.usage.cache.creation_input_tokens`; consumers following the canonical schema do not find these counts.
Emit the cache counts under the convention’s `cache.read_input_tokens` and `cache.creation_input_tokens` keys, while retaining any compatibility aliases only if they do not replace those canonical attributes.
Also at `sdk/go/instrumentation/anthropic/streaming.go:224-228`, `sdk/go/instrumentation/openai/instrumentor.go:444`, `sdk/go/semconv/genai.go:136-137`.
</issue_to_address>
### Comment 2
<location path="sdk/go/instrumentation/anthropic/instrumentor.go" line_range="204-208" />
<code_context>
semconv.SetIntAttribute(span, semconv.GenAIUsageTotalTokens, resp.Usage.InputTokens+resp.Usage.OutputTokens)
if resp.Usage.CacheCreationInputTokens > 0 {
semconv.SetIntAttribute(span, semconv.GenAIUsagePromptTokensDetailsCacheWrite, resp.Usage.CacheCreationInputTokens)
+ semconv.SetIntAttribute(span, semconv.GenAIUsageCacheCreationInputTokensKey, resp.Usage.CacheCreationInputTokens)
}
if resp.Usage.CacheReadInputTokens > 0 {
semconv.SetIntAttribute(span, semconv.GenAIUsagePromptTokensDetailsCacheRead, resp.Usage.CacheReadInputTokens)
+ semconv.SetIntAttribute(span, semconv.GenAIUsageCacheReadInputTokensKey, resp.Usage.CacheReadInputTokens)
}
}
</code_context>
<issue_to_address>
**Usage aliases are dropped at span limits**
When a caller configures a restrictive OpenTelemetry span attribute limit and earlier span attributes leave room for a legacy usage key but not its new aliases, `SetIntAttribute` adds each UI-facing key after its legacy counterpart. When the span’s attribute limit has room for the legacy key but not the additional aliases, OpenTelemetry drops the new keys and the UI still shows the usage counts as missing.
Set the UI-facing keys before compatibility aliases so they take precedence when span attribute limits are reached.
Also at `sdk/go/instrumentation/anthropic/streaming.go:224-228`, `sdk/go/instrumentation/openai/instrumentor.go:432-444`.
</issue_to_address>Sourcery assessment
Needs a human reviewer. 2 findings to address first, and if the new attributes contain incorrect token counts, exported spans may retain those values after a revert. The same counts are also emitted under existing attributes, so affected telemetry can be identified and corrected or recomputed; reverting stops the incorrect keys from being emitted in future spans.
Blocking findings: sdk/go/instrumentation/anthropic/instrumentor.go:208, sdk/go/instrumentation/anthropic/instrumentor.go:208
| semconv.SetIntAttribute(span, semconv.GenAIUsageCacheCreationInputTokensKey, resp.Usage.CacheCreationInputTokens) | ||
| } | ||
| if resp.Usage.CacheReadInputTokens > 0 { | ||
| semconv.SetIntAttribute(span, semconv.GenAIUsagePromptTokensDetailsCacheRead, resp.Usage.CacheReadInputTokens) | ||
| semconv.SetIntAttribute(span, semconv.GenAIUsageCacheReadInputTokensKey, resp.Usage.CacheReadInputTokens) |
There was a problem hiding this comment.
🟡 Medium · Canonical cache counts remain missing
When the Anthropic or OpenAI response includes cache-token usage, the new emitters write cache counts under gen_ai.usage.cache_read.input_tokens and gen_ai.usage.cache_creation.input_tokens rather than the convention’s gen_ai.usage.cache.read_input_tokens and gen_ai.usage.cache.creation_input_tokens; consumers following the canonical schema do not find these counts.
Emit the cache counts under the convention’s cache.read_input_tokens and cache.creation_input_tokens keys, while retaining any compatibility aliases only if they do not replace those canonical attributes.
Also at sdk/go/instrumentation/anthropic/streaming.go:224-228, sdk/go/instrumentation/openai/instrumentor.go:444, sdk/go/semconv/genai.go:136-137.
Prompt for AI agents
In `sdk/go/instrumentation/anthropic/instrumentor.go` at lines 204-208:
**Canonical cache counts remain missing**
When the Anthropic or OpenAI response includes cache-token usage, the new emitters write cache counts under `gen_ai.usage.cache_read.input_tokens` and `gen_ai.usage.cache_creation.input_tokens` rather than the convention’s `gen_ai.usage.cache.read_input_tokens` and `gen_ai.usage.cache.creation_input_tokens`; consumers following the canonical schema do not find these counts.
Emit the cache counts under the convention’s `cache.read_input_tokens` and `cache.creation_input_tokens` keys, while retaining any compatibility aliases only if they do not replace those canonical attributes.
Also at `sdk/go/instrumentation/anthropic/streaming.go:224-228`, `sdk/go/instrumentation/openai/instrumentor.go:444`, `sdk/go/semconv/genai.go:136-137`.From agent-guides/coding-agents-convention.md L3:
This is the **strict** contract between every coding-agent vendor and OpenLit. Every adapter, every native-telemetry mapping, every query has to match it exactly. Drift in either direction (renaming an attribute, switching units, adding ad-hoc vendor-namespaced keys) breaks the `/coding-agents` and `/agents` views in subtle, hard-to-diagnose ways. When in doubt, prefer this document over what any individual vendor's docs claim.| semconv.SetIntAttribute(span, semconv.GenAIUsageCacheCreationInputTokensKey, resp.Usage.CacheCreationInputTokens) | ||
| } | ||
| if resp.Usage.CacheReadInputTokens > 0 { | ||
| semconv.SetIntAttribute(span, semconv.GenAIUsagePromptTokensDetailsCacheRead, resp.Usage.CacheReadInputTokens) | ||
| semconv.SetIntAttribute(span, semconv.GenAIUsageCacheReadInputTokensKey, resp.Usage.CacheReadInputTokens) |
There was a problem hiding this comment.
🟡 Medium · Usage aliases are dropped at span limits
When a caller configures a restrictive OpenTelemetry span attribute limit and earlier span attributes leave room for a legacy usage key but not its new aliases, SetIntAttribute adds each UI-facing key after its legacy counterpart. When the span’s attribute limit has room for the legacy key but not the additional aliases, OpenTelemetry drops the new keys and the UI still shows the usage counts as missing.
Set the UI-facing keys before compatibility aliases so they take precedence when span attribute limits are reached.
Also at sdk/go/instrumentation/anthropic/streaming.go:224-228, sdk/go/instrumentation/openai/instrumentor.go:432-444.
Prompt for AI agents
In `sdk/go/instrumentation/anthropic/instrumentor.go` at lines 204-208:
**Usage aliases are dropped at span limits**
When a caller configures a restrictive OpenTelemetry span attribute limit and earlier span attributes leave room for a legacy usage key but not its new aliases, `SetIntAttribute` adds each UI-facing key after its legacy counterpart. When the span’s attribute limit has room for the legacy key but not the additional aliases, OpenTelemetry drops the new keys and the UI still shows the usage counts as missing.
Set the UI-facing keys before compatibility aliases so they take precedence when span attribute limits are reached.
Also at `sdk/go/instrumentation/anthropic/streaming.go:224-228`, `sdk/go/instrumentation/openai/instrumentor.go:432-444`.
Issue number: Fixes #1984
Change description:
The Go SDK wrote cache and reasoning token counts under attribute names that neither the Python SDK nor the OpenLIT UI read. This adds the names they do read and keeps the legacy keys, so existing consumers are unaffected.
Added to
sdk/go/semconv/genai.go(additions only, no existing constant changed):gen_ai.usage.cache_read.input_tokensandgen_ai.usage.cache_creation.input_tokens, the names in the OpenTelemetry GenAI conventions and the Python SDK. Anthropic non-streaming and streaming emit both, OpenAI non-streaming emits the read count.gen_ai.usage.reasoning.output_tokens,gen_ai.usage.reasoning_tokens(already a constant, reused) andgen_ai.usage.completion_tokens_details.reasoning_tokensfor OpenAI non-streaming reasoning counts.Why these keys: the UI reads
usage.cache_read.input_tokens,usage.cache_creation.input_tokensandusage.completion_tokens_details.reasoning_tokens(src/client/src/constants/traces.ts), and the span attribute is looked up as the flat keygen_ai.<path>. Nothing undersrc/reads a key the Go SDK emitted before.Things to know when reviewing:
GenAIUsageCacheReadInputTokensandGenAIUsageCacheCreationInputTokensalready exist but holdgen_ai.usage.cache.read_input_tokensandgen_ai.usage.cache.creation_input_tokens, which are not the standard names and are used by no instrumentation. I left them alone for API compatibility, so the new ones carry aKeysuffix.openai/streaming.gohas no cache or reasoning code, not even the legacy keys; that is a separate change.gen_ai.usage.reasoning.output_tokens.reportedor the derived completed-output-tokens attribute.completion_tokens_details.reasoning_tokensgoes beyond what Python's OpenAI path emits, because it is the only reasoning key the trace detail view reads.> 0guards. Python emits them even when 0, but the Go usage structs use plainintwithomitempty, so absent and zero cannot be told apart. The UI shows-for a missing key.cli/internal/coding/hook/cursor/handle.gohard-codes the non-standardgen_ai.usage.cache.read_input_tokens. Out of scope here.How was this tested
Tests assert the key strings as literals, so on the unmodified source they fail on a missing attribute rather than a missing symbol:
They pass with the change. Each emitted attribute was also removed on its own and each removal fails a test (12 mutations, none survived).
go vet ./...andgo test ./...pass insdk/go, andcd cli && go build ./...succeeds against the patched module.gofmt -llists four files that are already unformatted onmain(helpers/config.go,helpers/pricing.go,anthropic/types.go,openai/types.go); this PR does not touch them.-racecould not run on the machine I used.My other Go SDK PRs (#1982, #1983, #1985) touch the same test files, so whichever merges second needs a small rebase. I will do that.
Checklist
If your change doesn't seem to apply, please leave them unchecked.
Acknowledgment
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of the project license.
Summary by Sourcery
Align Go SDK token usage attributes with the keys consumed by downstream SDK and UI components.
Bug Fixes:
Enhancements:
Tests: