fix: SDK cost tracking — add claude-sonnet-4 pricing + model in span data#255
fix: SDK cost tracking — add claude-sonnet-4 pricing + model in span data#255
Conversation
Two bugs causing $0 cost and "unknown" model in dashboard:
1. claude-sonnet-4-20250514 and claude-opus-4-20250515 were missing
from the pricing table. All calls returned $0.00 with
UnknownModelWarning. Added both models.
2. patch_anthropic/patch_openai created spans with empty data={}.
Dashboard queries e.data->>'model' for cost-by-model aggregation,
which returned 'unknown'. Now passes data={"model": ...,
"provider": ...} when creating the trace span.
Both fixes apply to all 4 patch variants (sync/async x openai/anthropic).
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8229a68f86
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| """Shared traced wrapper for sync OpenAI create calls.""" | ||
| model = kwargs.get("model", "unknown") | ||
| with tracer.trace(f"llm.openai.{model}") as ctx: | ||
| with tracer.trace(f"llm.openai.{model}", data={"model": model, "provider": "openai"}) as ctx: |
There was a problem hiding this comment.
Serialize model before attaching it to span data
This now stores model in span data without sanitization, so a non-JSON-serializable model value (for example an object/enum passed through wrapper code) will make sink emission fail in TraceContext.__enter__ when JsonlFileSink/StdoutSink calls json.dumps(event), aborting the LLM call before original_create runs. Previously this path only interpolated model into the span name (stringified), so this commit introduces a new hard failure mode for malformed-but-commonly-seen dynamic inputs.
Useful? React with 👍 / 👎.
Summary
Autoresearch agent traces show $0 cost and "unknown" model in dashboard. Two root causes:
Missing model pricing —
claude-sonnet-4-20250514andclaude-opus-4-20250515not incost.pypricing table.estimate_cost()returned $0.00 withUnknownModelWarning.Empty span data —
patch_anthropic/patch_openaicreated trace spans withdata={}. Dashboard queriese.data->>'model'for cost-by-model aggregation, which returned "unknown". Now passesdata={"model": ..., "provider": ...}in all 4 patch variants (sync/async x openai/anthropic).Evidence
Before fix: autoresearch traces show "unknown" model and $0.00 cost
After fix:
estimate_cost("claude-sonnet-4-20250514", 1000, 500, "anthropic")returns$0.0105Test plan
🤖 Generated with Claude Code