new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

AI SDK · Providers · all subjects

anthropic/options

16 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Claude 4 extended thinking with thinking provider option

Claude 4 models support extended thinking capabilities through the `thinking` provider option. Extended thinking can be enabled by setting `thinking: { type: 'enabled', budgetTokens: <number> }` in the `providerOptions.anthropic` configuration. The budgetTokens parameter specifies a thinking budget in tokens. Both Opus 4 and Sonnet 4 support tool use during extended thinking, allowing Claude to alternate between reasoning and tool use.

Claude 4 interleaved thinking with beta header

For interleaved thinking where Claude can think in between tool calls, enable the beta feature by setting the 'anthropic-beta' header to 'interleaved-thinking-2025-05-14'. This allows Claude to use tools during extended thinking and alternate between reasoning and tool use to improve responses.

toUIMessageStream with sendReasoning option

The toUIMessageStream helper accepts a `sendReasoning: true` parameter to forward the model's reasoning tokens to the client. This allows the frontend to display reasoning information alongside the text response.

Claude 4 prompt engineering best practices

Claude 4 models respond well to: (1) explicit instructions that clearly state what the model should do including specific steps or response formats, (2) context and motivation explaining why a task is being performed to help the model understand underlying goals, and (3) positive examples demonstrating desired behavior rather than negative examples showing what to avoid.

Dynamic prompt caching reduces API costs for long conversations

Prompt caching allows caching of conversation prefixes to significantly reduce API costs for repeated context. This is particularly useful for building agents with long conversations and heavy tool usage.

Anthropic ephemeral cache cache control directive

Anthropic uses the cacheControl directive with type 'ephemeral' to cache conversation context. Mark the final block of the final message with cache_control so the conversation can be incrementally cached. The cache_control should be set at the content block level in the API request.

AI SDK translates message-level providerOptions to block-level cache_control

The AI SDK automatically translates providerOptions set at the message level to cache_control at the content block level when constructing the API request. When you set providerOptions on a message, the SDK applies it to the last content block. For example, setting providerOptions with anthropic.cacheControl at the message level results in cache_control being applied to the last content block in the request sent to Anthropic.

Block-level providerOptions take priority over message-level settings

If you need finer control over cache_control, you can set providerOptions directly on individual content parts, which takes priority over message-level settings.

addCacheControlToMessages utility function for Anthropic caching

The addCacheControlToMessages utility function applies Anthropic's cacheControl directive to messages. It accepts an object with messages (ModelMessage array), model (LanguageModel), and optional providerOptions. The function returns the messages unchanged if not an Anthropic model, or adds providerOptions with anthropic.cacheControl.type 'ephemeral' to the final message if it is an Anthropic model. The isAnthropicModel detection function checks if the model string includes 'anthropic' or 'claude', or if the model object has provider or modelId including these terms.

addCacheControlToMessages implementation

import type { ModelMessage, JSONValue, LanguageModel } from 'ai'; function isAnthropicModel(model: LanguageModel): boolean { if (typeof model === 'string') { return model.includes('anthropic') || model.includes('claude'); } return ( model.provider === 'anthropic' || model.provider.includes('anthropic') || model.modelId.includes('anthropic') || model.modelId.includes('claude') ); } export function addCacheControlToMessages({ messages, model, providerOptions = { anthropic: { cacheControl: { type: 'ephemeral' } }, }, }: { messages: ModelMessage[]; model: LanguageModel; providerOptions?: Record<string, Record<string, JSONValue>>; }): ModelMessage[] { if (messages.length === 0) return messages; if (!isAnthropicModel(model)) return messages; return messages.map((message, index) => { if (index === messages.length - 1) { return { ...message, providerOptions: { ...message.providerOptions, ...providerOptions, }, }; } return message; }); }

Using addCacheControlToMessages with generateText and stopWhen

Integrate the addCacheControlToMessages utility into your agent using the prepareStep callback with generateText and stopWhen. The prepareStep callback receives messages and model, and should return an object with the modified messages. Example: prepareStep: ({ messages, model }) => ({ messages: addCacheControlToMessages({ messages, model }) })

Complete example of dynamic prompt caching with generateText

import { anthropic } from '@ai-sdk/anthropic'; import { generateText, tool, isStepCount } from 'ai'; import { z } from 'zod'; import { addCacheControlToMessages } from './add-cache-control-to-messages'; async function main() { const result = await generateText({ model: anthropic('claude-sonnet-4-5'), prompt: 'Help me analyze this codebase and suggest improvements.', stopWhen: isStepCount(10), tools: { analyzeFile: tool({ description: 'Analyze a file in the codebase', inputSchema: z.object({ path: z.string().describe('Path to the file'), }), execute: async ({ path }) => { return { analysis: `Analysis of ${path}` }; }, }), }, prepareStep: ({ messages, model }) => ({ messages: addCacheControlToMessages({ messages, model }), }), }); console.log(result.text); } main().catch(console.error);

Anthropic prompt caching minimum token threshold

Anthropic requires a minimum number of tokens before caching activates. Short conversations may not benefit from caching.

Anthropic ephemeral cache has 5-minute TTL

Anthropic's ephemeral cache has a 5-minute time-to-live (TTL). Inactive conversations lose their cache after 5 minutes.

Anthropic cached token pricing and cost structure

With Anthropic, cached tokens cost 10% of input token cost. However, cache writes cost 25% more than regular input tokens. You save money when cache hits exceed cache misses.

What caching options are available with Claude's extended thinking

Claude supports prompt caching using the cacheControl directive with type 'ephemeral'. This can be applied at the message level using providerOptions.anthropic.cacheControl, and the AI SDK automatically translates this to block-level cache_control in the actual API request. The ephemeral cache has a 5-minute TTL, and cached tokens cost 10% of input token cost while cache writes cost 25% more.

Give your agent this brain