Skip to content

Commit c60b393

Browse files
feat(anthropic): add the new compaction feature (#12384)
## 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: - [x] `examples/ai-functions/src/generate-text/anthropic-compaction-pause.ts` - [x] `examples/ai-functions/src/generate-text/anthropic-compaction.ts` - [x] `examples/ai-functions/src/stream-text/anthropic-compaction.ts` - [x] `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 - [x] Tests have been added / updated (for bug fixes / features) - [ ] Documentation has been added / updated (for bug fixes / features) - [x] 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) ## Related Issues fixes #12297
1 parent 054ccd7 commit c60b393

21 files changed

+4307
-28
lines changed

.changeset/breezy-cooks-fly.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@ai-sdk/anthropic': patch
3+
---
4+
5+
feat(anthropic): add the new compaction feature

content/providers/01-ai-sdk-providers/05-anthropic.mdx

Lines changed: 106 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,98 @@ const result = await generateText({
290290
});
291291
```
292292

293+
#### Compaction
294+
295+
The `compact_20260112` edit type automatically summarizes earlier conversation context when token limits are reached. This is useful for long-running conversations where you want to preserve the essence of earlier exchanges while staying within token limits.
296+
297+
```ts highlight="7-19"
298+
import { anthropic, AnthropicProviderOptions } from '@ai-sdk/anthropic';
299+
import { streamText } from 'ai';
300+
301+
const result = streamText({
302+
model: anthropic('claude-opus-4-6'),
303+
messages: conversationHistory,
304+
providerOptions: {
305+
anthropic: {
306+
contextManagement: {
307+
edits: [
308+
{
309+
type: 'compact_20260112',
310+
trigger: {
311+
type: 'input_tokens',
312+
value: 50000, // trigger compaction when input exceeds 50k tokens
313+
},
314+
instructions:
315+
'Summarize the conversation concisely, preserving key decisions and context.',
316+
pauseAfterCompaction: false,
317+
},
318+
],
319+
},
320+
} satisfies AnthropicProviderOptions,
321+
},
322+
});
323+
```
324+
325+
**Configuration:**
326+
327+
- **trigger** - Condition that triggers compaction (e.g., `{ type: 'input_tokens', value: 50000 }`)
328+
- **instructions** - Custom instructions for how the model should summarize the conversation. Use this to guide the compaction summary towards specific aspects of the conversation you want to preserve.
329+
- **pauseAfterCompaction** - When `true`, the model will pause after generating the compaction summary, allowing you to inspect or process it before continuing. Defaults to `false`.
330+
331+
When compaction occurs, the model generates a summary of the earlier context. This summary appears as a text block with special provider metadata.
332+
333+
##### Detecting Compaction in Streams
334+
335+
When using `streamText`, you can detect compaction summaries by checking the `providerMetadata` on `text-start` events:
336+
337+
```ts
338+
for await (const part of result.fullStream) {
339+
switch (part.type) {
340+
case 'text-start': {
341+
const isCompaction =
342+
part.providerMetadata?.anthropic?.type === 'compaction';
343+
if (isCompaction) {
344+
console.log('[COMPACTION SUMMARY START]');
345+
}
346+
break;
347+
}
348+
case 'text-delta': {
349+
process.stdout.write(part.text);
350+
break;
351+
}
352+
}
353+
}
354+
```
355+
356+
##### Compaction in UI Applications
357+
358+
When using `useChat` or other UI hooks, compaction summaries appear as regular text parts with `providerMetadata`. You can style them differently in your UI:
359+
360+
```tsx
361+
{
362+
message.parts.map((part, index) => {
363+
if (part.type === 'text') {
364+
const isCompaction =
365+
(part.providerMetadata?.anthropic as { type?: string } | undefined)
366+
?.type === 'compaction';
367+
368+
if (isCompaction) {
369+
return (
370+
<div
371+
key={index}
372+
className="bg-yellow-100 border-l-4 border-yellow-500 p-2"
373+
>
374+
<span className="font-bold">[Compaction Summary]</span>
375+
<div>{part.text}</div>
376+
</div>
377+
);
378+
}
379+
return <div key={index}>{part.text}</div>;
380+
}
381+
});
382+
}
383+
```
384+
293385
#### Applied Edits Metadata
294386

295387
After generation, you can check which edits were applied in the provider metadata:
@@ -305,6 +397,9 @@ if (metadata?.appliedEdits) {
305397
} else if (edit.type === 'clear_thinking_20251015') {
306398
console.log(`Cleared ${edit.clearedThinkingTurns} thinking turns`);
307399
console.log(`Freed ${edit.clearedInputTokens} tokens`);
400+
} else if (edit.type === 'compact_20260112') {
401+
console.log('Compaction was applied');
402+
console.log(`Freed ${edit.clearedInputTokens} tokens`);
308403
}
309404
});
310405
}
@@ -1174,17 +1269,17 @@ and the `mediaType` should be set to `'application/pdf'`.
11741269

11751270
### Model Capabilities
11761271

1177-
| Model | Image Input | Object Generation | Tool Usage | Computer Use | Web Search | Tool Search |
1178-
| -------------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- |
1179-
| `claude-opus-4-6` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
1180-
| `claude-opus-4-5` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
1181-
| `claude-haiku-4-5` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | |
1182-
| `claude-sonnet-4-5` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
1183-
| `claude-opus-4-1` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | |
1184-
| `claude-opus-4-0` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | |
1185-
| `claude-sonnet-4-0` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | |
1186-
| `claude-3-7-sonnet-latest` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | |
1187-
| `claude-3-5-haiku-latest` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | |
1272+
| Model | Image Input | Object Generation | Tool Usage | Computer Use | Web Search | Tool Search | Compaction |
1273+
| -------------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- | ------------------- |
1274+
| `claude-opus-4-6` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
1275+
| `claude-opus-4-5` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | |
1276+
| `claude-haiku-4-5` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | | |
1277+
| `claude-sonnet-4-5` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | |
1278+
| `claude-opus-4-1` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | | |
1279+
| `claude-opus-4-0` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | | |
1280+
| `claude-sonnet-4-0` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | | |
1281+
| `claude-3-7-sonnet-latest` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | | |
1282+
| `claude-3-5-haiku-latest` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | | |
11881283

11891284
<Note>
11901285
The table above lists popular models. Please see the [Anthropic

0 commit comments

Comments
 (0)