Skip to content

@effect/ai-anthropic: responses whose usage has only input_tokens and output_tokens fail to decode (Anthropic-compatible gateways) #9963

Closed

Description

@jayhemnani9910

What version of Effect is running?

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
  • BetaMessageDeltaEvent.usage: cache_creation_input_tokens, cache_read_input_tokens, input_tokens

Additional information

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

  1. self-assigned this
    on Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions