From 68d65a5b687483a53f254b2c1a40b03122670e78 Mon Sep 17 00:00:00 2001 From: kerry Date: Thu, 1 Oct 2026 17:44:07 +0000 Subject: [PATCH] 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> --- tests/integration/_support/claude_code.py | 8 ++++++-- ...est_anthropic_cache_breakpoint_injection_wire.py | 12 ++++++++---- .../test_anthropic_cache_usage_response_wire.py | 13 ++++++++++++- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/tests/integration/_support/claude_code.py b/tests/integration/_support/claude_code.py index 9d80832f776..605ce7bd50d 100644 --- a/tests/integration/_support/claude_code.py +++ b/tests/integration/_support/claude_code.py @@ -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)) diff --git a/tests/integration/messages_endpoint/providers/anthropic/caching/test_anthropic_cache_breakpoint_injection_wire.py b/tests/integration/messages_endpoint/providers/anthropic/caching/test_anthropic_cache_breakpoint_injection_wire.py index bca8e67b68c..f0629feff13 100644 --- a/tests/integration/messages_endpoint/providers/anthropic/caching/test_anthropic_cache_breakpoint_injection_wire.py +++ b/tests/integration/messages_endpoint/providers/anthropic/caching/test_anthropic_cache_breakpoint_injection_wire.py @@ -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: diff --git a/tests/integration/messages_endpoint/providers/anthropic/caching/test_anthropic_cache_usage_response_wire.py b/tests/integration/messages_endpoint/providers/anthropic/caching/test_anthropic_cache_usage_response_wire.py index afd950c6af4..fcb6d1af3cf 100644 --- a/tests/integration/messages_endpoint/providers/anthropic/caching/test_anthropic_cache_usage_response_wire.py +++ b/tests/integration/messages_endpoint/providers/anthropic/caching/test_anthropic_cache_usage_response_wire.py @@ -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: