Repository navigation
Description
Problem
cache_control on a system part never reaches Anthropic. The request still returns 200, so nothing looks wrong, but every turn pays full input tokens for the system prompt.
# 1. Cache a large allergen policy; add a per-table rule each turn
res = await ai.generate(
model=Anthropic.claude('claude-sonnet-4-6'),
system=[Part.from_text(ALLERGEN_MANUAL, metadata={'cache_control': {'type': 'ephemeral'}})],
messages=[
Message(role=Role.SYSTEM, content=[Part.from_text('Table 4 avoids peanuts.')]),
Message(role=Role.USER, content=[Part.from_text('Suggest an appetizer.')]),
],
)
# 2. What goes out on POST /v1/messages today
# => "system": "<ALLERGEN_MANUAL>\n\nTable 4 avoids peanuts."
# cache_control is gone; usage shows no cache writes or readsAnthropicModel says it supports "Prompt caching via cache_control metadata on content parts" (_models.py:316). That holds for user and model parts, but system parts lose it.
There's no workaround: extra={'system': [...]} raises INVALID_ARGUMENT because system is in _MANAGED_BODY_FIELDS (_models.py:235).
Mechanism
_to_anthropic_messagesskipsRole.SYSTEM(_models.py:727), so theget_cache_controlpass at line 736 never runs on system parts._extract_system(_models.py:707) joins the system text into a singlestrand throws away part metadata.- The JSON fallback concatenates its schema instruction onto that string (
_models.py:561). Even if caching worked, a cached prefix would share a block with text that varies from call to call.
Proposed fix
Send system as list[TextBlockParam], which messages.create already accepts (system: str | Iterable[TextBlockParam]):
# => "system": [
# {"type": "text", "text": "<ALLERGEN_MANUAL>", "cache_control": {"type": "ephemeral"}},
# {"type": "text", "text": "Table 4 avoids peanuts."}
# ]- One text block per non-empty system text part, in order, with
cache_controlcopied from part metadata. - Skip empty text. The API rejects empty text blocks.
- The JSON fallback instruction goes in as its own trailing block instead of being string-concatenated.
- Pin it through
ai.generate+FakeClaudeApiinanthropic_generate_test.py: cached system part, multiple system messages, empty system text, and JSON fallback.
Users don't see this change. The value is private and Anthropic accepts both shapes. Internal tests that do '...' in params['system'] will need updating. Users who already cache on user turns get one cache miss after the upgrade, because the rendered prefix changes.
References: Go's Anthropic plugin already sends every system message as a TextBlockParam. JS forwards cache_control on system blocks. Python Bedrock keeps cachePoint on system blocks (genkit_amazon_bedrock/_converters.py, _system_blocks).
Related: #146 keeps every system message, joined as a string. This issue is the follow-up and doesn't block that PR.
Activity
ssbushi commentedon Nov 18, 2026
@genkit-bot
genkit-bot commentedon Nov 18, 2026
Triage: P2 · S · implement: AnthropicModel._extract_system flattens only the first system message into a plain string and drops cache_control metadata on system parts.
Area: python
Root cause:
AnthropicModel._extract_systeminpackages/genkit-anthropic/src/genkit_anthropic/_models.pyjoins system text parts from only the firstRole.SYSTEMmessage into a plainstrand discardspart.metadata['cache_control'], while_build_paramsstring-concatenates the JSON fallback instruction ontosystem.Plan:
_extract_systeminpackages/genkit-anthropic/src/genkit_anthropic/_models.pyto collect non-empty text parts across allRole.SYSTEMmessages as{'type': 'text', 'text': ...}block dicts and attachcache_controlviaget_cache_control(part)._build_paramsas its own trailing text block instead of concatenating strings.uv run --no-sync pytest packages/genkit-anthropic/tests, adding unit andFakeClaudeApitests inanthropic_models_test.pyandanthropic_generate_test.pyfor cached system parts, multiple system messages, empty system text, and JSON fallback blocks.Details
packages/genkit-anthropic/src/genkit_anthropic/_models.py:_extract_system(lines 707-716) returns early on the firstRole.SYSTEMmessage and never callsget_cache_control(part), unlike_to_anthropic_messages(lines 736-738).packages/genkit-anthropic/src/genkit_anthropic/_models.py:_build_params(lines 535-564) buildssystemand appends the JSON fallback instruction by string concatenation.packages/genkit-anthropic/tests/anthropic_models_test.py:test_extract_system(line 209) and structured-output fallback tests (lines 710-828) assert againstparams['system']as a string and will need updating for block lists.Run
8d7d4149-e6ed-46ec-aae6-8c163bb931f5· Cloud Run logs