Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/anthropic-native-compaction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@tanstack/ai': minor
'@tanstack/ai-compaction': minor
'@tanstack/ai-anthropic': minor
---

`withCompaction({ native })` now passes the tools, the model options, and the reasoning of the chat call into `compact`. The Anthropic text adapter implements `compact` as an on-demand Messages request (`compaction: { type: "summarize" }`, beta `compact-2026-09-04`). The signed compaction block stays on the assistant message and goes back on later requests to the same model. A result with no signed block uses the configured strategy.
32 changes: 32 additions & 0 deletions docs/adapters/anthropic-advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,38 @@ export const throughProxy = anthropicText("claude-opus-5-5", {

If that endpoint sends the request and the `anthropic-beta` header on to Anthropic without changes, set `true`. See [Mid-Conversation Changes](../advanced/mid-conversation-changes).

## Native compaction

Anthropic can compact a long conversation on its side. `createAnthropicChat` and `anthropicText` have a `compact` method for that call. Pass the adapter as `native`:

```typescript
import { chat } from "@tanstack/ai";
import { createAnthropicChat } from "@tanstack/ai-anthropic";
import { withCompaction } from "@tanstack/ai-compaction";

const adapter = createAnthropicChat(
"claude-opus-5-5",
process.env.ANTHROPIC_API_KEY!,
);

const stream = chat({
adapter,
messages: [{ role: "user", content: "Hello!" }],
threadId: "thread-1",
middleware: [withCompaction({ maxTokens: 100_000, native: adapter })],
});
```

The call is a Messages request. It sets `compaction` to `{ type: "summarize" }` and sends the beta `compact-2026-09-04`.

- The request includes the system prompts, the tools, and `max_tokens` from the chat. The call does not run a tool.
- The result is one assistant message. The message text is the summary.
- `metadata.anthropicCompaction` holds the signed block. The block has `content` and `signature`.
- The next request to the same model includes that block and the same beta.
- A result with no signed block uses `strategy`. After a 404, a 405, or a 501, that adapter and model use `strategy` until the process restarts.

See [Let the provider compact](../advanced/compaction#let-the-provider-compact).

## Provider tools

Anthropic has tools of its own, next to the function tools that you define. Import them from `@tanstack/ai-anthropic/tools` and pass them into `chat({ tools: [...] })`.
Expand Down
4 changes: 2 additions & 2 deletions docs/adapters/anthropic.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ Now a declined request gets an answer from a fallback model. When no model can a
These topics are on [Anthropic: Advanced](./anthropic-advanced):

- Bearer and OAuth tokens, gateways, and your own Anthropic client.
- Claude on Vertex AI.
- The `max_tokens` default and prompt caching.
- Claude on Vertex AI, the `max_tokens` default, and prompt caching.
- [Native compaction](./anthropic-advanced#native-compaction).
- Every provider tool, with a full sample.
- The API reference.

Expand Down
2 changes: 1 addition & 1 deletion docs/advanced/compaction-advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ These rules apply when you pass `native` (see [Let the provider compact](./compa
- When `compact` fails, `strategy` runs in its place. `onCompact` and `compaction:ended` get the failure in `error`. Some providers on the OpenAI Responses base have no compaction endpoint.
- After a 404, 405, or 501 (the provider has no compaction endpoint), that adapter object and model use `strategy` until the process restarts. Create the adapter once, outside the request handler, so that no more compaction calls go out.
- After any other failure, such as a 400, a 429, or a network error, the next compaction tries `compact` again.
- The compaction call gets the system prompts of the `chat()` call. It does not get the tools, because the compaction endpoint does not accept them.
- The compaction call gets the system prompts, the tools, the model options, and the reasoning of the `chat()` call.
- The compacted messages can hold an encrypted provider item. The adapter sends it back unchanged to the same model. Another model does not get it.

## Give the retry something to cut
Expand Down
13 changes: 12 additions & 1 deletion docs/advanced/compaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,18 @@ export async function POST(request: Request) {
- An adapter with no `compact` method uses `strategy`.
- When `compact` fails, `strategy` runs in its place.

The OpenAI Responses adapters have `compact`. See [Native compaction](../adapters/openai-advanced#native-compaction). For what happens on each kind of failure, see [Native compaction failures](./compaction-advanced#native-compaction-failures).
The Anthropic adapters have `compact` too. Create the adapter once:

```typescript
import { createAnthropicChat } from "@tanstack/ai-anthropic";

const adapter = createAnthropicChat(
"claude-opus-5-5",
process.env.ANTHROPIC_API_KEY!,
);
```

Pass that adapter to `chat()` and to `native`, as in the sample above. See [OpenAI native compaction](../adapters/openai-advanced#native-compaction) and [Anthropic native compaction](../adapters/anthropic-advanced#native-compaction). For each kind of failure, see [Native compaction failures](./compaction-advanced#native-compaction-failures).

## When the chat is still too long

Expand Down
8 changes: 4 additions & 4 deletions docs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -450,13 +450,13 @@
"label": "Compaction",
"to": "advanced/compaction",
"addedAt": "2026-08-24",
"updatedAt": "2026-10-22"
"updatedAt": "2026-11-09"
},
{
"label": "Compaction: Advanced",
"to": "advanced/compaction-advanced",
"addedAt": "2026-10-21",
"updatedAt": "2026-11-07"
"updatedAt": "2026-11-09"
}
],
"tab": "guides"
Expand Down Expand Up @@ -1724,13 +1724,13 @@
"label": "Anthropic",
"to": "adapters/anthropic",
"addedAt": "2026-04-15",
"updatedAt": "2026-10-22"
"updatedAt": "2026-11-09"
},
{
"label": "Anthropic: Advanced",
"to": "adapters/anthropic-advanced",
"addedAt": "2026-10-21",
"updatedAt": "2026-11-02"
"updatedAt": "2026-11-09"
},
{
"label": "Google Gemini",
Expand Down
52 changes: 50 additions & 2 deletions docs/reference/interfaces/TextCompactOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ id: TextCompactOptions
title: TextCompactOptions
---

Defined in: [packages/ai/src/types.ts:1570](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L1570)
Defined in: [packages/ai/src/types.ts:1584](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L1584)

What `TextAdapter.compact` receives.

Expand Down Expand Up @@ -62,6 +62,54 @@ The system prompts of the chat call.
optional wrapFetch?: FetchWrapper;
```

Defined in: [packages/ai/src/types.ts:1576](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L1576)
Defined in: [packages/ai/src/types.ts:1591](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L1591)

Wraps the fetch of the compaction request.

***

### tools?

```ts
optional tools?: AnyTool[];
```

Defined in: [packages/ai/src/types.ts:1598](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L1598)

The tools of the chat call. The provider reads the definitions. The compaction call does not run a tool.

***

### modelOptions?

```ts
optional modelOptions?: Record<string, unknown>;
```

Defined in: [packages/ai/src/types.ts:1600](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L1600)

Provider model options of the chat call, including `max_tokens`.

***

### reasoning?

```ts
optional reasoning?: ReasoningRequest;
```

Defined in: [packages/ai/src/types.ts:1602](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L1602)

How hard the model thinks on the chat call.

***

### addUsage?

```ts
optional addUsage?: (usage: TokenUsage) => void;
```

Defined in: [packages/ai/src/types.ts:1604](https://github.com/TanStack/ai/blob/main/packages/ai/src/types.ts#L1604)

Report the token usage of the compaction call.
Loading
Loading