Skip to content

feat(anthropic): add the new compaction feature#12384

Merged
aayush-kapoor merged 11 commits intomainfrom
aayush/anthropic-compaction
Feb 10, 2026
Merged

feat(anthropic): add the new compaction feature#12384
aayush-kapoor merged 11 commits intomainfrom
aayush/anthropic-compaction

Conversation

@aayush-kapoor
Copy link
Copy Markdown
Collaborator

@aayush-kapoor aayush-kapoor commented Feb 9, 2026

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

  • updated the schema
  • updated the usage token calculation since the outer level params don't account for the compaction tokens

Manual Verification

verified by running the examples:

  • examples/ai-functions/src/generate-text/anthropic-compaction-pause.ts
  • examples/ai-functions/src/generate-text/anthropic-compaction.ts
  • examples/ai-functions/src/stream-text/anthropic-compaction.ts
  • http://localhost:3000/use-chat-anthropic-compaction
    • large context is preloaded into the model
    • just send a simple follow up message and you'll notice the compaction block created (also noticed in the console logs)
    • multi-turn conversations work properly

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 #12297

@vercel-ai-sdk vercel-ai-sdk bot added ai/core core functions like generateText, streamText, etc. Provider utils, and provider spec. ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label provider/anthropic Issues related to the @ai-sdk/anthropic provider labels Feb 9, 2026
console.log(`Freed ${edit.clearedInputTokens} tokens`);
} else if (edit.type === 'compact_20260112') {
console.log('Compaction was applied');
console.log(`Freed ${edit.clearedInputTokens} tokens`);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log(`Freed ${edit.clearedInputTokens} tokens`);

Documentation code example for compact_20260112 edit references edit.clearedInputTokens which does not exist on the compact edit type, producing Freed undefined tokens at runtime.

Fix on Vercel

Copy link
Copy Markdown
Contributor

@leog25 leog25 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, approved

@aayush-kapoor aayush-kapoor merged commit c60b393 into main Feb 10, 2026
18 checks passed
@aayush-kapoor aayush-kapoor deleted the aayush/anthropic-compaction branch February 10, 2026 16:03
@shaper shaper added the backport Admins only: add this label to a pull request in order to backport it to the prior version label Feb 10, 2026
vercel-ai-sdk bot pushed a commit that referenced this pull request Feb 10, 2026
@vercel-ai-sdk vercel-ai-sdk bot removed the backport Admins only: add this label to a pull request in order to backport it to the prior version label Feb 10, 2026
@vercel-ai-sdk
Copy link
Copy Markdown
Contributor

vercel-ai-sdk bot commented Feb 10, 2026

⚠️ Backport to release-v5.0 created but has conflicts: #12404

gr2m pushed a commit that referenced this pull request Feb 10, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai/core core functions like generateText, streamText, etc. Provider utils, and provider spec. ai/provider related to a provider package. Must be assigned together with at least one `provider/*` label provider/anthropic Issues related to the @ai-sdk/anthropic provider

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for anthropic's new server side compaction

4 participants