Skip to content

Conversation

@shubham-021
Copy link
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.

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

Copy link
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
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
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
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
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
Contributor Author

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

@shubham-021
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anthropic: implement context_management

3 participants