Skip to content

Normalize xAI token usage before persistence - #26

Merged
K-Mistele merged 4 commits into
mainfrom
fix-xai-negative-usage
Aug 28, 2026
Merged

Normalize xAI token usage before persistence#26
K-Mistele merged 4 commits into
mainfrom
fix-xai-negative-usage

Conversation

@K-Mistele

@K-Mistele K-Mistele commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Summary

  • normalize xAI's text-only completion_tokens at the OpenAI-compatible client boundary before Effect derives token details
  • use total_tokens to distinguish xAI semantics from providers whose completion count already includes reasoning
  • retain defensive durable-usage normalization so malformed optional provider details cannot fail a completed turn
  • cover both xAI-style and already-inclusive usage payloads

Root cause

xAI reports completion_tokens as text output while separately reporting reasoning_tokens. Effect's OpenAI-compatible adapter expects completion_tokens to include reasoning and subtracts reasoning to derive text usage, which produced a negative value and failed schema validation after model output.

Verification

  • live grok-4.6 request through makeXaiLanguageModel returned nonnegative text, reasoning, and total usage
  • bun run typecheck
  • bun run test
  • bun run lint
  • bun run format:check

@K-Mistele K-Mistele changed the title Fix malformed provider usage metadata Normalize xAI token usage before persistence Aug 28, 2026
@K-Mistele
K-Mistele merged commit 6a76cd8 into main Aug 28, 2026
5 checks passed
@K-Mistele
K-Mistele deleted the fix-xai-negative-usage branch August 28, 2026 19:24
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.

1 participant