Skip to content

feat(anthropic): context management#10540

Merged
lgrammel merged 23 commits intovercel:mainfrom
shubham-021:feat/anthropic_context_management
Dec 8, 2025
Merged

feat(anthropic): context management#10540
lgrammel merged 23 commits intovercel:mainfrom
shubham-021:feat/anthropic_context_management

Conversation

@shubham-021
Copy link
Copy Markdown
Contributor

@shubham-021 shubham-021 commented Nov 24, 2025

Background

Anthropic's now supports context management to automatically clear conversation history when approaching token limits. This prevents model_context_window_exceeded errors and enables longer conversations by removing older tool uses or thinking blocks while preserving recent context.

See https://platform.claude.com/docs/en/build-with-claude/context-editing

Summary

Added the context management feature for anthropic:

  • Added context_management configuration to provider options
  • Implemented clear_tool_uses_20250919 and clear_thinking_20251015 strategies
  • Parse and expose applied context edits in response metadata
  • Map model_context_window_exceeded stop reason to 'length'
  • Added tests and updated snapshots

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

@vercel-ai-sdk vercel-ai-sdk bot added 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 Nov 24, 2025
@gr2m gr2m self-assigned this Nov 24, 2025
Copy link
Copy Markdown
Collaborator

@gr2m gr2m left a comment

Choose a reason for hiding this comment

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

could you please add an example for manual testing? e.g. examples/ai-core/src/generate-text/anthropic-context-management.ts

Also the ## Manual Verification section in your pull request templates is very important. For this particular case, I would add or alter an existing example to examples/next-openai (ignore the openai in the name, we have lots of other provider examples in there) and then test it extensively with multi-turn chats

@shubham-021 shubham-021 force-pushed the feat/anthropic_context_management branch from 834eb48 to d403f29 Compare November 24, 2025 22:42
@lgrammel
Copy link
Copy Markdown
Collaborator

a) code style seems to be off, unclear why prettier is not running
b) adding a section to the anthropic provider docs content/providers/01-ai-sdk-providers/05-anthropic.mdx would be very useful

@shubham-021 shubham-021 force-pushed the feat/anthropic_context_management branch from 1d04abb to 91c8ca6 Compare November 25, 2025 13:59
@shubham-021
Copy link
Copy Markdown
Contributor Author

a) code style seems to be off, unclear why prettier is not running b) adding a section to the anthropic provider docs content/providers/01-ai-sdk-providers/05-anthropic.mdx would be very useful

I have applied the changes to the doc and replaced the if-else logic with switch case , but i can not say about the issue regarding prettier , i ran pnpm prettier-fix on my machine , and it ran successfully.

@shubham-021 shubham-021 force-pushed the feat/anthropic_context_management branch from 32753a7 to 927049f Compare November 25, 2025 14:40
@shubham-021 shubham-021 requested review from gr2m and lgrammel November 26, 2025 07:58
@gr2m
Copy link
Copy Markdown
Collaborator

gr2m commented Nov 29, 2025

@shubham-021 hey just a quick note: please don't force-push changes once we started reviewing, it will make us loose track of what we already reviewed and what changed since the last review

@gr2m
Copy link
Copy Markdown
Collaborator

gr2m commented Nov 29, 2025

could you help us resolving the conflict?

Resolved conflicts in anthropic-messages-language-model.ts by:
- Keeping context management variable declaration and logic
- Preserving upstream URL variable and other changes
- Maintaining context management parsing in doGenerate() and doStream()

All tests passing.
@shubham-021
Copy link
Copy Markdown
Contributor Author

@gr2m i have updated the branch , this should do now

@shubham-021
Copy link
Copy Markdown
Contributor Author

@gr2m I have resolved the conflicts again , can you please review this ?


default:
throw new Error(
`Unknown strategy: ${strategy}`,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

can you add a warning insterad of throwing an error

@lgrammel
Copy link
Copy Markdown
Collaborator

lgrammel commented Dec 7, 2025

Looks pretty close, please add a couple of unit tests and also change the error to a warning

@lgrammel
Copy link
Copy Markdown
Collaborator

lgrammel commented Dec 8, 2025

Please do not change the branch any more; I'll make a few last edits so we can get this merged.

@lgrammel lgrammel changed the title feat(anthropic) : implement context_management feat(anthropic) : context management Dec 8, 2025
@lgrammel lgrammel changed the title feat(anthropic) : context management feat(anthropic): context management Dec 8, 2025
@shubham-021
Copy link
Copy Markdown
Contributor Author

Please do not change the branch any more; I'll make a few last edits so we can get this merged.

I modified the type AnthropicResponseClearToolUsesEdit in order to implement 'warning' in place of throwing an error , thats what causing this issue .

@lgrammel lgrammel merged commit b2dbfbf into vercel:main Dec 8, 2025
17 of 18 checks passed
@shubham-021 shubham-021 deleted the feat/anthropic_context_management branch December 10, 2025 10:46
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/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.

Anthropic: implement context_management

3 participants