feat(anthropic): add the new compaction feature#12384
Merged
aayush-kapoor merged 11 commits intomainfrom Feb 10, 2026
Merged
Conversation
felixarntz
approved these changes
Feb 9, 2026
Contributor
|
|
gr2m
pushed a commit
that referenced
this pull request
Feb 10, 2026
## Background backport for #12384
This was referenced Feb 12, 2026
4 tasks
aayush-kapoor
added a commit
that referenced
this pull request
Feb 20, 2026
…ma (#12471) ## Background PR #12384 introduced support for Anthropic's compaction feature, but the streaming Zod schema for compaction_delta events requires content to be a non-nullable string. Anthropic's API sends compaction_delta events with content: null (e.g. the initial frame before the compaction summary text), which causes Zod validation to fail at runtime. ## Summary Changed content: z.string() to content: z.string().nullish() in the compaction_delta streaming schema in packages/anthropic/src/anthropic-messages-api.ts. This matches the existing content_block_start schema for compaction, which already uses .nullish(). ## Manual Verification Reproduction requires streaming a conversation that exceeds the compaction trigger threshold (50k+ tokens) with @ai-sdk/anthropic, which triggers a compaction_delta event with content: null. This is not practical to verify without an API key and a large conversation, but the added regression test simulates this exact scenario by streaming a compaction_delta chunk with content: null. ## Checklist - Tests have been added / updated (for bug fixes / features) - Documentation has been added / updated (for bug fixes / features) - A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root) - I have reviewed this pull request (self-review) ## Related Issues Fixes #12470 --------- Co-authored-by: Embedder <215220128+embedder-dev@users.noreply.github.com> Co-authored-by: Aayush Kapoor <83492835+aayush-kapoor@users.noreply.github.com>
vercel-ai-sdk bot
pushed a commit
that referenced
this pull request
Feb 20, 2026
…ma (#12471) ## Background PR #12384 introduced support for Anthropic's compaction feature, but the streaming Zod schema for compaction_delta events requires content to be a non-nullable string. Anthropic's API sends compaction_delta events with content: null (e.g. the initial frame before the compaction summary text), which causes Zod validation to fail at runtime. ## Summary Changed content: z.string() to content: z.string().nullish() in the compaction_delta streaming schema in packages/anthropic/src/anthropic-messages-api.ts. This matches the existing content_block_start schema for compaction, which already uses .nullish(). ## Manual Verification Reproduction requires streaming a conversation that exceeds the compaction trigger threshold (50k+ tokens) with @ai-sdk/anthropic, which triggers a compaction_delta event with content: null. This is not practical to verify without an API key and a large conversation, but the added regression test simulates this exact scenario by streaming a compaction_delta chunk with content: null. ## Checklist - Tests have been added / updated (for bug fixes / features) - Documentation has been added / updated (for bug fixes / features) - A patch changeset for relevant packages has been added (for bug fixes / features - run pnpm changeset in the project root) - I have reviewed this pull request (self-review) ## Related Issues Fixes #12470 --------- Co-authored-by: Embedder <215220128+embedder-dev@users.noreply.github.com> Co-authored-by: Aayush Kapoor <83492835+aayush-kapoor@users.noreply.github.com>
felixarntz
added a commit
that referenced
this pull request
Feb 20, 2026
…ntext editing (#12547) ## Background Anthropic context management support (`clear_tool_uses`, `clear_thinking`) was added in #10540 (with a related fix in #12154), and compaction support (`compact`) was added in #12384. ## Summary With the complexities of handling these features correctly (including how `clear_tool_uses` and `clear_thinking` can be combined), I think it makes sense to add some more tests and also examples around this. - Adds fixture based tests for context editing (tool uses, thinking, and both combined), each for `generateText` and `streamText` - Adds examples for thinking, compaction, and thinking and tool uses combined (there already was an example for tool uses alone, which I renamed for consistency) - Expands docs, adding "Context Editing" grouping section for both `clear_thinking` and `clear_tool_uses` ## Manual Verification All examples were run against the live Anthropic API to verify correct behavior, and the fixture data was captured from those real API responses. ## Checklist - [x] Tests have been added / updated (for bug fixes / features) - [x] Documentation has been added / updated (for bug fixes / features) - [ ] A _patch_ changeset for relevant packages has been added (for bug fixes / features - run `pnpm changeset` in the project root) - [x] I have reviewed this pull request (self-review) ## Future Work N/A ## Related Issues N/A
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
anthropic released a new compaction feature along with their launch of Opus 4.6
https://platform.claude.com/docs/en/build-with-claude/compaction
Summary
Manual Verification
verified by running the examples:
examples/ai-functions/src/generate-text/anthropic-compaction-pause.tsexamples/ai-functions/src/generate-text/anthropic-compaction.tsexamples/ai-functions/src/stream-text/anthropic-compaction.tshttp://localhost:3000/use-chat-anthropic-compactionChecklist
pnpm changesetin the project root)Related Issues
fixes #12297