-
Notifications
You must be signed in to change notification settings - Fork 3.4k
feat(anthropic): context management #10540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lgrammel
merged 23 commits into
vercel:main
from
shubham-021:feat/anthropic_context_management
Dec 8, 2025
+1,017
−160
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
88cefa1
feat/context_management
shubham-021 fc3201d
test/add_tests_for_context_management
shubham-021 0af1195
fix/snake_case to camelCase
shubham-021 54f6a0c
add example for anthropic context management
shubham-021 8dec952
fix and resolve conflicts
shubham-021 270d08d
fix(style): Use camelCase for contextManagement
shubham-021 120dba8
add doc and refactor to switch
shubham-021 927049f
docs: Fix incorrect field names
shubham-021 c68af2e
Merge upstream/main into feat/anthropic_context_management
shubham-021 7d66285
Merge branch 'main' into feat/anthropic_context_management
shubham-021 c312c18
Merge upstream/main into feat/anthropic_context_management
shubham-021 abf5d8c
prettier-fix
shubham-021 faf04a0
Merge branch 'main' into feat/anthropic_context_management
lgrammel 7c61c56
Merge branch 'main' into feat/anthropic_context_management
shubham-021 3003c4c
warning instead of throwing an error
shubham-021 777ec2e
add unit tests for context management request mapping
shubham-021 1824f46
prettier-fix
shubham-021 bbdc915
d
lgrammel fde6270
x
lgrammel 173de35
x
lgrammel cace34f
x
lgrammel 5328737
rm
lgrammel 86cc883
e
lgrammel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@ai-sdk/anthropic': patch | ||
| --- | ||
|
|
||
| add context_management for anthropic |
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
114 changes: 114 additions & 0 deletions
114
examples/ai-core/src/generate-text/anthropic-context-management.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import { anthropic, AnthropicProviderOptions } from '@ai-sdk/anthropic'; | ||
| import { generateText, tool } from 'ai'; | ||
| import { z } from 'zod'; | ||
| import { run } from '../lib/run'; | ||
|
|
||
| run(async () => { | ||
| const result = await generateText({ | ||
| model: anthropic('claude-haiku-4-5'), | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| content: 'What is the weather in San Francisco?', | ||
| }, | ||
| { | ||
| role: 'assistant', | ||
| content: [ | ||
| { | ||
| type: 'tool-call', | ||
| toolCallId: 'tool_1', | ||
| toolName: 'weather', | ||
| input: { location: 'San Francisco' }, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| role: 'tool', | ||
| content: [ | ||
| { | ||
| type: 'tool-result', | ||
| toolCallId: 'tool_1', | ||
| toolName: 'weather', | ||
| output: { | ||
| type: 'json', | ||
| value: { temperature: 72, condition: 'sunny' }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| role: 'user', | ||
| content: 'What about New York?', | ||
| }, | ||
| { | ||
| role: 'assistant', | ||
| content: [ | ||
| { | ||
| type: 'tool-call', | ||
| toolCallId: 'tool_2', | ||
| toolName: 'weather', | ||
| input: { location: 'New York' }, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| role: 'tool', | ||
| content: [ | ||
| { | ||
| type: 'tool-result', | ||
| toolCallId: 'tool_2', | ||
| toolName: 'weather', | ||
| output: { | ||
| type: 'json', | ||
| value: { temperature: 65, condition: 'cloudy' }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| role: 'user', | ||
| content: 'compare the two cities.', | ||
| }, | ||
| ], | ||
| tools: { | ||
| weather: tool({ | ||
| description: 'Get the weather of a location', | ||
| inputSchema: z.object({ | ||
| location: z.string().describe('The location to get the weather for'), | ||
| }), | ||
| execute: async ({ location }) => ({ | ||
| location, | ||
| temperature: 72 + Math.floor(Math.random() * 21) - 10, | ||
| condition: 'sunny', | ||
| }), | ||
| }), | ||
| }, | ||
| providerOptions: { | ||
| anthropic: { | ||
| contextManagement: { | ||
| edits: [ | ||
| { | ||
| type: 'clear_tool_uses_20250919', | ||
| trigger: { | ||
| type: 'input_tokens', | ||
| value: 1000, | ||
| }, | ||
| keep: { | ||
| type: 'tool_uses', | ||
| value: 1, | ||
| }, | ||
| clearAtLeast: { | ||
| type: 'input_tokens', | ||
| value: 500, | ||
| }, | ||
| clearToolInputs: true, | ||
| excludeTools: ['important_tool'], | ||
| }, | ||
| ], | ||
| }, | ||
| } satisfies AnthropicProviderOptions, | ||
| }, | ||
| }); | ||
|
|
||
| console.log('request body:', JSON.stringify(result.request.body, null, 2)); | ||
| }); |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.