4 Commits
Author SHA1 Message Date
Alex CastilloandOwen 68fedc4252 fix(ai-gateway): read Bedrock invoke-with-response-stream usage from base64 chunks
invoke-with-response-stream wraps each model event as {"bytes":"<base64>"} inside event-stream frames, so the usage fields never appear in clear text. extractUsage returned null and the pipeline fell back to estimating tokens over the base64, losing real counts and cache tokens. Decode the chunks before scanning.
2026-11-09 16:23:48 -05:00
Alex Castillo 4932b8be2c fix(ai): read usage from response.incomplete in OpenAI Responses streams
extractOpenAiResponses only took response.usage from response.completed, so a
stream cut by max_output_tokens (response.incomplete) lost its usage and fell
back to estimateUsage over the whole SSE text, over-counting cost and budget.
Read response.usage from any event that carries it.
2026-11-09 04:04:08 -04:00
Alex Castillo 64c9c85fc1 fix(ai): count Gemini toolUsePromptTokenCount as input tokens
Gemini reports the tokens of tool results (Google Search, URL context) in usageMetadata.toolUsePromptTokenCount, separate from promptTokenCount. The extractor ignored it, so usage and cost were under-counted.
2026-11-05 19:02:15 -04:00
Alex Castillo 61c89834e8 fix(ai-gateway): stop subtracting Bedrock Converse cache reads from inputTokens
With prompt caching enabled, Bedrock reports inputTokens as the non-cached input only; cacheReadInputTokens and cacheWriteInputTokens are separate counters. Subtracting the cache reads again undercounted promptTokens (clamped to 0) in both the JSON and the stream-scan paths.
2026-11-02 19:57:39 -05:00