Skip to content

Test that each agent request extends the one before it - #663

Merged
AshishKumar4 merged 1 commit into
mainfrom
prompt-prefix-test
Oct 5, 2026
Merged

AshishKumar4 merged 1 commit into
mainfrom
prompt-prefix-test

Conversation

@AshishKumar4

@AshishKumar4 AshishKumar4 commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

Anthropic and OpenAI cache a prompt by its prefix. A request reads the cache up to the first point where it differs from an earlier request, and pays again for everything after that. So each agent request in a chat must start with all of the request before it.

Request 2 starts with request 1's tools, system prompt and user message, read from the cache, then adds the tool call and result.

This PR adds an integration test for that rule. It runs two turns against the scripted model, one of them with a tool call. For each request, the test checks that:

  • all fields other than messages are the same as in the previous request, and
  • the previous request's messages come first.

The test compares serialized JSON, so a key in a different order also fails it.

Check: I added Date.now() to the system prompt, and the test failed on the first request of turn 2.

Known gap: a turn that changes the workspace still breaks the prefix, because the system prompt lists the gadgets and their files. #665 fixes that and adds that case to this test.

Providers cache a prompt by its prefix, so a request that doesn't start with the whole previous request pays again for everything after the first difference. The test runs two turns, one with a tool step, and checks that each request keeps every field but its messages unchanged and starts its messages with those of the request before.
@ask-bonk

ask-bonk Bot commented Oct 5, 2026

Copy link
Copy Markdown

LGTM!

github run

@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown

Preview: pr663-prompt-prefix-test

https://pr663-prompt-prefix-test-router.cloudflare-os-previews.workers.dev

Dashboard · deleted when this PR closes

@AshishKumar4
AshishKumar4 merged commit eaa76d6 into main Oct 5, 2026
14 checks passed
@AshishKumar4
AshishKumar4 deleted the prompt-prefix-test branch October 5, 2026 19:48
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