Put a stable tool list at the start of each request so the provider can keep its prompt cache. Add a tool that appears mid-chat after that prefix.
On some GPT and Claude models, TanStack AI sends the added tool inside the conversation. The start stays the same, and the cache keeps working. You change no code.
This setup takes about 5 minutes.
The channel exists on these models:
| Adapter | Models |
|---|---|
| openaiText | gpt-5.4-mini, gpt-5.5, gpt-5.6-luna, gpt-5.6-sol, gpt-5.6-terra, gpt-6-astra, gpt-6-luna, gpt-6-sol |
| anthropicText | claude-opus-4-8, claude-opus-5, claude-opus-5-5, claude-fable-5, claude-fable-5-1 |
Every other model and adapter sends the full lists on each call.
This route starts with one tool. A middleware adds a second tool after the first model call:
import {
chat,
chatParamsFromRequest,
toServerSentEventsResponse,
toolDefinition,
} from "@tanstack/ai";
import type { ChatMiddleware } from "@tanstack/ai";
import { openaiText } from "@tanstack/ai-openai";
import { z } from "zod";
const getWeather = toolDefinition({
name: "get_weather",
description: "Get the current weather for a city",
inputSchema: z.object({ city: z.string() }),
}).server(async ({ city }) => ({ city, temperature: 21 }));
const getForecast = toolDefinition({
name: "get_forecast",
description: "Get the forecast for a city",
inputSchema: z.object({ city: z.string() }),
}).server(async ({ city }) => ({ city, tomorrow: "sunny" }));
const addForecast: ChatMiddleware = {
name: "add-forecast",
onConfig: (ctx, config) => {
if (ctx.phase !== "beforeModel" || ctx.iteration === 0) return;
if (config.tools.some((tool) => tool.name === "get_forecast")) return;
return { tools: [...config.tools, getForecast] };
},
};
export async function POST(request: Request) {
const { messages, threadId } = await chatParamsFromRequest(request);
const stream = chat({
adapter: openaiText("gpt-6-astra"),
messages,
threadId,
tools: [getWeather],
middleware: [addForecast],
});
return toServerSentEventsResponse(stream);
}The client does not change. Ask about the weather, and the second model call reads the start of the request from the cache.
The second model call of the example sends this:
| Model | tools | The new tool |
|---|---|---|
| gpt-6-astra | get_weather only | An additional_tools item after the tool result |
| claude-opus-5-5 | get_weather, a placeholder tool, then get_forecast with defer_loading: true | A system message with a tool_addition block after the tool result |
| A model with no channel, such as gpt-6.1-sol | get_weather and get_forecast | Nothing extra |
Some more rules:
To check in code, read midConversationChannels on the adapter:
import { openaiText } from "@tanstack/ai-openai";
const adapter = openaiText("gpt-6-astra");
console.log(adapter.midConversationChannels); // { tools: true, systemPrompts: true }It is undefined when the model has no channel, or when the channels are off.
The channels are on by default only when the adapter talks to the API of the provider directly. A gateway or a proxy can drop the extra fields. So the channels are off with:
Set midConversationChannels in the adapter config to choose:
import { anthropicText } from "@tanstack/ai-anthropic";
import { openaiText } from "@tanstack/ai-openai";
// Send the full lists on every call.
export const fullLists = openaiText("gpt-6-astra", {
midConversationChannels: false,
});
// Use the channels behind a proxy that sends the request on with no change.
export const throughProxy = anthropicText("claude-opus-5-5", {
baseURL: "https://llm-proxy.example.com",
midConversationChannels: true,
});On a model with no channel, true changes nothing.
Before each model call, chat() compares the tools and the system prompts with the earlier calls. It saves a small record on the first assistant message of the call, in the midConversationChange field. The record holds tool names and short prompt hashes, not tool definitions.
The next turn reads these records:
These changes still break the cache one time: