Skip to content

fix(py/anthropic): bound sampling config and map cache usage - #70

Merged
huangjeff5 merged 3 commits into
mainfrom
fix/py-anthropic-sampling-bounds-cache-usage
Nov 11, 2026
Merged

huangjeff5 merged 3 commits into
mainfrom
fix/py-anthropic-sampling-bounds-cache-usage

Conversation

@huangjeff5

@huangjeff5 huangjeff5 commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Two Anthropic plugin bugs. An out-of-range temperature or top_p was sent to Anthropic and came back as a provider HTTP 400. And on prompt-cached calls, response.usage undercounted: input_tokens held only the uncached tail of the prompt, and the shared cache fields stayed empty, so cost dashboards read a 1,884-token prompt as 40 tokens.

# --- BEFORE ---
# 1. Out-of-range sampling went over the network and failed at the provider
await ai.generate(
    model='anthropic/claude-sonnet-4-6',
    prompt='Suggest a dish.',
    config={'temperature': 1.4},
)
# => error from an Anthropic HTTP 400 round-trip

# 2. Cached call (Anthropic raw usage: input_tokens=40, cache_read=1834, cache_creation=10, output_tokens=20)
u = response.usage
print(u.input_tokens, u.total_tokens, u.cached_content_tokens, u.cache_write_tokens)
# => 40 60 None None

# --- AFTER ---
# 1. Fails locally during config validation, no request sent
await ai.generate(
    model='anthropic/claude-sonnet-4-6',
    prompt='Suggest a dish.',
    config={'temperature': 1.4},
)
# => GenkitError: INVALID_ARGUMENT: temperature must be between 0.0 and 1.0, got 1.4

# 2. input_tokens is the full prompt; cache reads and writes are subsets of it
u = response.usage
print(u.input_tokens, u.total_tokens, u.cached_content_tokens, u.cache_write_tokens)
# => 1884 1904 1834 10

Changes

  • Sampling bounds checked before the request: AnthropicConfig declares common_field_bounds = {'temperature': (0.0, 1.0), 'top_p': (0.0, 1.0)}. Bad values raise INVALID_ARGUMENT locally, which Retry middleware does not retry, and the Dev UI config form now shows the minimum/maximum. Vertex AI Model Garden's Claude models reuse AnthropicConfig and get the same bounds.
  • Cache tokens counted in input_tokens and total_tokens: Anthropic's raw input_tokens excludes tokens read from or written to the prompt cache. build_cache_usage now adds both back, sets cached_content_tokens (reads) and cache_write_tokens (writes), and leaves them None when zero. Anthropic's raw keys stay on usage.custom. ai.generate and ai.generate_stream go through the same _build_usage and report identical usage.

Breaking changes

None. The old input_tokens was wrong for cached calls; uncached calls report the same numbers as before.

Cross-SDK

The Anthropic config JSON Schema the Dev UI reads now has "minimum": 0.0, "maximum": 1.0 on temperature and topP. Usage in model responses and trace spans changes for cached calls:

// before
{"inputTokens":40,"outputTokens":20,"totalTokens":60,"custom":{"cache_creation_input_tokens":10,"cache_read_input_tokens":1834}}
// after
{"inputTokens":1884,"outputTokens":20,"totalTokens":1904,"cachedContentTokens":1834,"cacheWriteTokens":10,"custom":{"cache_creation_input_tokens":10,"cache_read_input_tokens":1834}}
  • Go / JS / Dart: no schema change; cachedContentTokens and cacheWriteTokens are already in genkit-schema.json. Inclusive inputTokens is what Go, the Gemini and OpenAI plugins, and OpenTelemetry GenAI already report. JS Anthropic still reports the uncached tail in inputTokens; the JS owner may want the same fix.

Declare common_field_bounds ([0.0, 1.0]) on AnthropicConfig for temperature and top_p so out-of-range values fail locally with INVALID_ARGUMENT and advertise minimum/maximum in the Dev UI schema.

Map Anthropic cache_read_input_tokens and cache_creation_input_tokens onto cached_content_tokens and cache_write_tokens, and include both in input_tokens and total_tokens so cached counts are subsets of input_tokens (matching Go, Gemini, OpenAI, and OpenTelemetry GenAI) while preserving Anthropic's raw keys on usage.custom.
@huangjeff5
huangjeff5 force-pushed the fix/py-anthropic-sampling-bounds-cache-usage branch from 118cba4 to f83525e Compare November 11, 2026 02:11

@pavelgj pavelgj left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@huangjeff5
huangjeff5 enabled auto-merge (squash) November 11, 2026 16:45
@huangjeff5
huangjeff5 merged commit ceeb5dd into main Nov 11, 2026
20 checks passed
@huangjeff5
huangjeff5 deleted the fix/py-anthropic-sampling-bounds-cache-usage branch November 11, 2026 17:32
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