You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
@effect/ai-anthropic: responses whose usage has only input_tokens and output_tokens fail to decode (Anthropic-compatible gateways) #9963
effect and @effect/ai-anthropic from main at 16fe800.
What steps can reproduce the bug?
Point AnthropicClient.layer({ apiKey, apiUrl }) at an Anthropic-compatible gateway that sends a minimal usage object, and call LanguageModel.generateText or LanguageModel.streamText. I used freellmapi's /v1/messages, which answers with:
// non-streaming message, and message_start in a stream"usage": { "input_tokens": 3, "output_tokens": 1 }
// message_delta"usage": { "output_tokens": 1 }
What is the expected behavior?
The response decodes. These fields only feed cache accounting, which already falls back when they are missing or null (cache_creation_input_tokens ?? 0).
What do you see instead?
generateText: AnthropicClient.createMessage: Invalid response: Missing key
at ["usage"]["cache_creation"]
streamText: AnthropicClient.createMessageStream: Invalid response: Missing key
at [0]["data"]["message"]["usage"]["cache_creation"]
Adding the missing keys one by one, the generated schemas require:
BetaMessage.usage and BetaMessageStartEvent.message.usage: cache_creation, cache_creation_input_tokens, cache_read_input_tokens, service_tier
Same kind of problem as #8464 (inference_geo), which was fixed with codegen.yml replacements. If these keys become optional, the message_delta handler needs a change too: it checks Predicate.isNotNull(event.usage.input_tokens) (AnthropicLanguageModel.ts:2209, and the two cache fields below it), and undefined passes that check, so a delta without input_tokens would set the input count to undefined. Predicate.isNotNullish avoids that. Happy to open a PR with both if that helps.
Activity