test(anthropic): assert final streamed cache usage and string system prompt body

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
kerry 2026-10-01 17:44:07 +00:00
parent 3f6bef3c34
commit 68d65a5b68
3 changed files with 26 additions and 7 deletions

View file

@ -958,7 +958,11 @@ def message_reply(
def message_stream(
identity: str, model: str, content: tuple[dict[str, JsonValue], ...], usage: dict[str, JsonValue]
identity: str,
model: str,
content: tuple[dict[str, JsonValue], ...],
usage: dict[str, JsonValue],
final_usage: Mapping[str, JsonValue] | None = None,
) -> tuple[bytes, ...]:
start: Final = sse_frame(
"message_start",
@ -981,7 +985,7 @@ def message_stream(
{
"type": "message_delta",
"delta": {"stop_reason": "end_turn", "stop_sequence": None},
"usage": _delta_usage(usage),
"usage": _delta_usage(usage) if final_usage is None else dict(final_usage),
},
)
blocks: Final = chain.from_iterable(_block_frames(index, block) for index, block in enumerate(content))

View file

@ -80,10 +80,14 @@ def test_prompt_caching_key_adds_breakpoints_to_the_system_prompt_and_last_messa
def test_prompt_caching_key_turns_a_string_system_prompt_into_a_cached_block(gateway: Gateway) -> None:
sent: Final = {**_unmarked_claude_code_turn(), "system": "Synthetic agent identity system prompt."}
_, body, _ = _forward(gateway, sent, {"enable_prompt_caching": True}, {})
assert body["system"] == [
{"type": "text", "text": "Synthetic agent identity system prompt.", "cache_control": _FIVE_MINUTES}
]
forwarded, _, _ = _forward(gateway, sent, {"enable_prompt_caching": True}, {})
assert forwarded.breakpoints == {"system[0]": _FIVE_MINUTES, "messages[0].content[7]": _FIVE_MINUTES}
assert forwarded.other_changes == {
"system": {
"expected": "Synthetic agent identity system prompt.",
"upstream": [{"type": "text", "text": "Synthetic agent identity system prompt."}],
}
}
def test_key_without_prompt_caching_adds_no_breakpoints(gateway: Gateway) -> None:

View file

@ -24,7 +24,11 @@ def test_streamed_cache_read_and_write_tokens_reach_the_client_unchanged(gateway
def respond(request: Request) -> Reply:
return Reply(
chunks=cc.message_stream(
f"msg_{uuid.uuid4().hex}", _MODEL, ({"type": "text", "text": "PONG"},), _ANTHROPIC_USAGE
f"msg_{uuid.uuid4().hex}",
_MODEL,
({"type": "text", "text": "PONG"},),
_ANTHROPIC_USAGE,
final_usage=_ANTHROPIC_USAGE,
),
content_type="text/event-stream",
)
@ -40,6 +44,13 @@ def test_streamed_cache_read_and_write_tokens_reach_the_client_unchanged(gateway
"cache_creation_input_tokens": 300,
"cache_creation": {"ephemeral_5m_input_tokens": 100, "ephemeral_1h_input_tokens": 200},
}, response.text
assert cc.streamed_usage(response.text) == {
"input_tokens": 100,
"output_tokens": 50,
"cache_read_input_tokens": 400,
"cache_creation_input_tokens": 300,
"cache_creation": {"ephemeral_5m_input_tokens": 100, "ephemeral_1h_input_tokens": 200},
}, response.text
def test_non_streamed_cache_read_and_write_tokens_reach_the_client_unchanged(gateway: Gateway) -> None: