[BUG] Wrong Session ID for subsequent sessions in open telemetry
Environment
- Platform (select one):
- [x] Anthropic API
- [ ] AWS Bedrock
- [ ] Google Vertex AI
- [ ] Other: <!-- specify -->
Environment info: darwin, iTerm.app, v1.0.31
Bug Description
Description
Claude Code is sending OpenTelemetry metrics with incorrect session IDs, causing all metrics to be
attributed to an old session instead of the current active session. This results in incorrect
cost tracking and metrics attribution.
Steps to Reproduce
- Start a Claude Code session (Session A)
- Use Claude Code for a while, then end the session
- Start a new Claude Code session (Session B)
- Check the metrics being sent - they will have Session A's ID instead of Session B's ID
Expected Behavior
- Each metric should be tagged with the session ID of the currently active session
- When a new session starts, all metrics should use the new session's ID
Actual Behavior
- Metrics continue to use an old/stale session ID even after new sessions have started
- Logs correctly use the new session ID, but metrics do not
- This causes all costs and metrics to accumulate under one session
Evidence
From production telemetry data:
- Session 2314baaf-98d6-4c51-a3cf-bb45858ead84 had activity from 10:06-12:06 (based on logs)
- Later sessions c34f8d9e-4ef1-40e0-8787-3805dd0a4734 (18:42-18:52) and
9a689904-9eab-47bc-80d0-ae3e670ecf1a (18:24-18:41) have logs but zero metrics
- All metrics sent during 18:25-18:52 are incorrectly tagged with the old session ID
2314baaf-98d6-4c51-a3cf-bb45858ead84
Impact
- Cost tracking shows $0 for all sessions except the first one
- Unable to accurately track metrics per session
- Dashboards and analytics show incorrect data
Additional Information
- Logs are correctly tagged with session IDs (both use session.id attribute)
- Only metrics have this issue
- The issue appears to be that the session ID is cached/stored somewhere and not updated when a
new session starts
Suggested Fix
Check where session ID is stored/cached for metrics emission and ensure it's updated when a new
session begins. The session ID should be fetched fresh for each metric or properly updated when
the session changes.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗