Skip to content

fix: Correct xAI cache field name (input_tokens_details) - #73

Merged
finml-sage merged 1 commit into
mainfrom
fix-cache-field-name
Mar 29, 2026
Merged

fix: Correct xAI cache field name (input_tokens_details)#73
finml-sage merged 1 commit into
mainfrom
fix-cache-field-name

Conversation

@nexus-marbell

Copy link
Copy Markdown
Collaborator

xAI returns cached_tokens under input_tokens_details, not prompt_tokens_details. Debug log from #72 confirmed:

{"input_tokens": 84054, "input_tokens_details": {"cached_tokens": 1216}, ...}

Three locations fixed. Cache metrics will now surface correctly in logs and Anthropic usage response.

Note: cache hit rate is low (~1.4%) because xAI prefix caching only matches the exact start of the request. The system prompt (~1216 tokens) caches, but tool definitions and conversation history change each turn, breaking the prefix match early. This is expected behavior per xAI docs.

🤖 Generated with Claude Code

…okens_details

xAI returns cached_tokens under input_tokens_details, not
prompt_tokens_details. Debug logging confirmed the actual field name.
Cache was working but we weren't reading the metrics.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

@finml-sage finml-sage left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. input_tokens_details not prompt_tokens_details — xAI diverges from OpenAI naming. Cache confirmed working (1,216/84,054 cached). +3/-3.

@finml-sage
finml-sage merged commit 171260f into main Mar 29, 2026
nexus-marbell added a commit that referenced this pull request Apr 20, 2026
The bridge migrated from Chat Completions to the xAI Responses API in
PRs #55, #56, #57, #58 (issue #51). The README still described the
legacy wire format throughout, so a new reader landed on documentation
that did not match the code. This brings the README back in sync with
what is actually deployed on main.

Changes
- Architecture diagram now shows translation targeting the Responses API
- "Responses API as the Default" subsection added, referencing the migration PRs
- Protocol Translation table rewritten for Anthropic <-> Responses mappings
  (input/output, function_call / function_call_output, max_output_tokens,
  reasoning.effort, cached_tokens via input_tokens_details)
- "How It Works" module list includes responses_forward/reverse/streaming,
  model_routing, shared, enrichment_folding, and the handlers/ package;
  legacy Chat Completions modules are listed and labeled as such
- Project Structure tree updated to match the current repo layout
  (handlers/, bridge/, enrichment_folding.py, shared.py, structure_loader.py)
- Configuration table adds XAI_USE_CHAT_COMPLETIONS, IDENTITY_ENABLED,
  LOG_LEVEL, DUMP_REQUESTS, DUMP_DIR, sourced from .env.example
- GROK_MODEL default corrected to grok-4.20-reasoning-latest
  (TranslationConfig.default_model in translation/config.py)
- New "Prompt Caching" subsection documents the x-grok-conv-id affinity
  header, 90% cached-token discount, and cached_tokens telemetry
  (PRs #71, #73, cache diagnostics on main)
- Test count corrected from 235+/490+ to 713 (pytest --collect-only)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.

2 participants