CH 04 · Model-Provider Abstraction and Multi-Model Strategy · 13/15
Read as one pageReal code: Anthropic maps one reasoning level
One portable reasoning level goes in, and a provider-specific request shape comes out. On LanguageModelV4CallOptions, reasoning is provider-default, none, minimal, low, medium, high, xhigh, or max. resolveAnthropicReasoningConfig maps that word onto the object Anthropic will accept, and it clamps a level the model cannot take. doGenerate and doStream both reach the function through getArgs and prepareRequest. prepareRequest calls it only when the caller picked a level other than provider-default, and only when effort is not already set on the provider options.
function resolveAnthropicReasoningConfig({
// ... (17 lines: the reasoning level, the model id, and the capability flags)
}): Pick<AnthropicLanguageModelOptions, 'thinking' | 'effort'> | undefined {
if (!isCustomReasoning(reasoning)) { // [1]
return undefined;
}
if (reasoning === 'none') { // [2]
// `between_tools` is the lowest thinking setting on models that support
// it: no upfront thinking, only short progress notes between tool calls.
if (supportsBetweenToolsThinking) {
return { thinking: { type: 'between_tools' } };
}
// ... (13 lines: a model that rejects a disabled switch gets effort 'low' and a warning)
return { thinking: { type: 'disabled' } };
}
if (supportsAdaptiveThinking) { // [3]
// ... (12 lines: map minimal to low, and xhigh to xhigh or max)
return {
thinking: { type: 'adaptive', display: 'summarized' },
effort,
};
}
const budgetTokens = mapReasoningToProviderBudget({ // [4]
// ... (4 lines: the level, the model's output cap, and the warning list)
});
if (budgetTokens == null) {
return undefined;
}
return { thinking: { type: 'enabled', budgetTokens } };
}
Source: vercel/ai, packages/anthropic/src/anthropic-language-model.ts (Apache-2.0)
What to notice
- [1]
isCustomReasoning, inpackages/provider-utils, is false whenreasoningis missing or'provider-default'. The function then returnsundefined, so the shared flag adds no thinking field of its own. Which of the later returns runs is decided by capability flags fromgetModelCapabilities(modelId). - [2]
'none'becomes one of three request shapes. When the model can leave short notes between tool calls, the return is{ thinking: { type: 'between_tools' } }, which means no thinking up front. The cut lines returneffort: 'low'plus a warning when a disabled switch would be rejected, and{ thinking: { type: 'disabled' } }when the model accepts that switch. Thedisabledvalue is still sent. The comment besidesendThinkingsays some models default thinking on, so omitting the field would leave thinking on and spend the output budget. - [3] When the model uses adaptive thinking, the result is
{ type: 'adaptive', display: 'summarized' }plus an effort word frommapReasoningToProviderEffort. The helper maps a requested level onto a word that model accepts, and it warns when the word it sends differs from the one the caller asked for. - [4] Every other model gets a token budget.
mapReasoningToProviderBudgettakes a share of the model's output cap, then clamps that share to a floor of 1024 and to the cap itself. The return is{ thinking: { type: 'enabled', budgetTokens } }.
Read it yourself: In getModelCapabilities, list the flags that change the 'none' return. Then read the comment beside sendThinking and note why a disabled switch is still written into the request.
Finished this idea?
Your learning progress stays in this browser.