Share one `instructions` string across a conversation's requests instead of keeping a copy per request

Merged
DO
10d ago
+90-9

No linked issue: follow-up to #13221 and #13307, from an audit of clai2's memory use.

Why we make these changes

Every ModelRequest records the instructions it was sent with in ModelRequest.instructions, and each request joins them into a new string. So a conversation held one copy of its instructions per request. On a real clai2 session, 386 requests each held their own copy of the same instructions, 39 MB in all.

Requests with equal instructions now share one string:

  • when the agent sets a new request's instructions, it reuses the latest earlier request's string if they are equal;
  • when ModelMessagesTypeAdapter loads a history, it makes equal instructions strings one object.

[!WARNING]
Compatibility impact: ModelMessagesTypeAdapter is now built with an AfterValidator, so the public-API check reports its definition as changed. Its type, JSON schema, serialization and validated values are unchanged. The one difference: in a loaded history, requests with equal instructions now hold the same string object, so code that compares them with is sees them as identical.

Why this can ship in a minor release: Strings are immutable and every value is unchanged; only object identity differs.

Migration: None needed. Compare instructions with ==, not is.

New public surface

None. ModelMessagesTypeAdapter keeps its type, TypeAdapter[list[ModelMessage]], and its JSON schema.

User-visible behavior

Values are unchanged; only how many copies are kept changes. Measured on a real clai2 resume:

instructions string objectsRSS
main386382 MB
this PR1351 MB

Verification

  • test_requests_with_equal_instructions_share_one_string checks that a run's requests share one string, and that a history loaded from JSON and then continued still does. It fails on main.
  • The public-API check's ModelMessagesTypeAdapter finding is waived against v2.69.0 in .github/api-compatibility-waivers/377773cf….json.
  • Not changed: the on_change='append' window path in _instructions.py also joins a string, but _apply_instruction_parts replaces it with the shared one afterwards, and I found no per-request copies there.

What changes for existing users

Long conversations use less memory. Nothing else changes.

PR decisions

2026-11-12 · pre-push review 1 of 3 at 251394b20a2141239ebc02e91385fc594c095512 · iter -

2026-11-12 · pre-push review 1 of 3 at 251394b20a2141239ebc02e91385fc594c095512 · iter -

  • Decision: remediated: early return for requests without instructions; docstring names the history argument. Not extended to the append-window path (_instructions.py:769): _apply_instruction_parts replaces that string afterwards, and no copies were reproduced there
  • Why: reviewed tree 35eb02e47c; scope b8e111d1fa..251394b20a
  • Source: https://github.com/pydantic/pydantic-ai/tree/core-shared-instructions
  • Session: 3c952642-1824-455b-9e24-2786bcd67c8d

2026-11-12 · pre-push review 2 of 3 at 322b6a9995ad2760627892339d57f2abee5729e4 · iter -

2026-11-12 · pre-push review 2 of 3 at 322b6a9995ad2760627892339d57f2abee5729e4 · iter -

Checklist

  • No breaking changes in accordance with the version policy.
  • Any permitted compatibility impact has a label, warning, migration and release note, and an exact API-check waiver if the gate reports it.
  • PR title is fit for the release changelog.

🤖 Generated with Claude Code


Summary by cubic

Conversations no longer keep a copy of the instructions string per request, cutting memory in long sessions. On a real clai2 resume, 386 requests each held their own copy of the same instructions (39 MB); a new request now reuses an earlier request's string when equal, and ModelMessagesTypeAdapter deduplicates equal strings when loading a history. RSS on that resume dropped from 382 MB to 351 MB with no change to values or the public surface. Adds a regression test covering sharing, including after a history is loaded and resumed. An API compatibility waiver (moved to the v2.74.0 baseline) covers the added AfterValidator, whose type, schema, and values are unchanged.