feat(anthropic): context management#10540
Conversation
gr2m
left a comment
There was a problem hiding this comment.
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
834eb48 to
d403f29
Compare
examples/ai-core/src/generate-text/anthropic-context-management.ts
Outdated
Show resolved
Hide resolved
|
a) code style seems to be off, unclear why prettier is not running |
1d04abb to
91c8ca6
Compare
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. |
32753a7 to
927049f
Compare
|
@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 |
|
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.
|
@gr2m i have updated the branch , this should do now |
|
@gr2m I have resolved the conflicts again , can you please review this ? |
|
|
||
| default: | ||
| throw new Error( | ||
| `Unknown strategy: ${strategy}`, |
There was a problem hiding this comment.
can you add a warning insterad of throwing an error
packages/anthropic/src/__snapshots__/anthropic-messages-language-model.test.ts.snap
Show resolved
Hide resolved
|
Looks pretty close, please add a couple of unit tests and also change the error to a warning |
|
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 . |
…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
Background
Anthropic's now supports context management to automatically clear conversation history when approaching token limits. This prevents
model_context_window_exceedederrors 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:
context_managementconfiguration to provider optionsclear_tool_uses_20250919andclear_thinking_20251015strategiesmodel_context_window_exceededstop reason to'length'Checklist
pnpm changesetin the project root)Related Issues
Fixes #10485