Open History to see the last commit
1/**
2 * Claude thinking configuration and Anthropic-hosted tools (issue #77).
3 *
4 * Thinking: the Models API capability tree states which `thinking.type`s a
5 * model accepts and its effort levels, but not whether thinking can be
6 * turned off. The troubleshooting page's per-model table does ("Default:
7 * Always on"), keyed by display name. A parse of zero rows throws.
8 *
9 * Tools: the tool pages state model support in prose that differs page
10 * to page, so the table below is hand-written from them. Types the tool
11 * reference lists with no model limit apply to every listed model.
12 */
13import { assertParsed, cachedDocs, markdownTableRows } from './model-facts.ts'
14import { fetchText, sha256Text } from './types.ts'
15import type { FactSource, ModelFactSources, ModelReasoning } from './types.ts'
16import type { Fetcher } from './us-egress.ts'
17
18export const ANTHROPIC_THINKING_URL =
19 'https://platform.claude.com/docs/en/build-with-claude/thinking-troubleshooting.md'
20
21const TOOL_DOCS = 'https://platform.claude.com/docs/en/agents-and-tools'
22
23/** Display name (lowercased) → thinking cannot be turned off. */
24export function parseThinkingTable(markdown: string): Map<string, boolean> {
25 const out = new Map<string, boolean>()
26 for (const [model = '', types, defaultCell] of markdownTableRows(markdown)) {
27 if (!model.startsWith('Claude ') || !types || !defaultCell) continue
28 out.set(model.toLowerCase(), defaultCell.trim() === 'Always on')
29 }
30 return out
31}
32
33interface Supported {
34 supported?: boolean
35}
36
37export interface AnthropicThinkingCaps {
38 thinking?: Supported & {
39 types?: { enabled?: Supported; adaptive?: Supported }
40 }
41 effort?: Supported & Record<string, unknown>
42}
43
44/**
45 * `adaptive` wins when both are accepted: extended (`budget_tokens`) is
46 * deprecated on the models that still take it.
47 */
48export function anthropicReasoning(
49 caps: AnthropicThinkingCaps | undefined,
50 alwaysOn: boolean,
51): ModelReasoning | null {
52 const types = caps?.thinking?.types
53 if (!caps?.thinking?.supported || !types) return null
54 const mode = types.adaptive?.supported
55 ? 'adaptive'
56 : types.enabled?.supported
57 ? 'budget'
58 : null
59 if (!mode) return null
60 const efforts = Object.entries(caps.effort ?? {})
61 .filter(
62 ([, value]) =>
63 typeof value === 'object' &&
64 value !== null &&
65 (value as Supported).supported === true,
66 )
67 .map(([level]) => level)
68 return {
69 mode,
70 mandatory: alwaysOn,
71 ...(efforts.length > 0 ? { efforts } : {}),
72 }
73}
74
75/** Every listed model (the tool reference names no model limit). */
76const ANY = null
77
78// ponytail: hand-written from the tool pages on 2026-09-25. New models get only the
79// unrestricted types until a row names them; re-verify on each launch.
80const TOOLS: Array<{
81 types: Array<string>
82 models: Array<string> | null
83 page: string
84}> = [
85 {
86 types: [
87 'web_search_20250305',
88 'web_search_20260209',
89 'web_search_20260318',
90 ],
91 models: ANY,
92 page: 'tool-use/web-search-tool',
93 },
94 {
95 types: ['web_fetch_20250910'],
96 models: ANY,
97 page: 'tool-use/web-fetch-tool',
98 },
99 {
100 // "Dynamic filtering is available with Claude Fable 5.1, …"
101 types: ['web_fetch_20260209', 'web_fetch_20260309', 'web_fetch_20260318'],
102 models: [
103 'claude-fable-5-1',
104 'claude-mythos-5-1',
105 'claude-fable-5',
106 'claude-mythos-5',
107 'claude-opus-4-8',
108 'claude-opus-4-7',
109 'claude-opus-4-6',
110 'claude-sonnet-5',
111 'claude-sonnet-4-6',
112 ],
113 page: 'tool-use/web-fetch-tool',
114 },
115 {
116 // "Every supported model accepts all three tool versions."
117 types: [
118 'code_execution_20250825',
119 'code_execution_20260120',
120 'code_execution_20260521',
121 ],
122 models: [
123 'claude-fable-5-1',
124 'claude-mythos-5-1',
125 'claude-fable-5',
126 'claude-mythos-5',
127 'claude-opus-5-5',
128 'claude-opus-5',
129 'claude-opus-4-8',
130 'claude-opus-4-7',
131 'claude-opus-4-6',
132 'claude-opus-4-5-20251101',
133 'claude-sonnet-5',
134 'claude-sonnet-4-6',
135 'claude-sonnet-4-5-20250929',
136 'claude-haiku-4-5-20251001',
137 ],
138 page: 'tool-use/code-execution-tool',
139 },
140 {
141 // Executors in the model-compatibility table.
142 types: ['advisor_20260301'],
143 models: [
144 'claude-haiku-4-5-20251001',
145 'claude-sonnet-4-6',
146 'claude-sonnet-5',
147 'claude-opus-4-6',
148 'claude-opus-4-7',
149 'claude-opus-4-8',
150 'claude-opus-5',
151 'claude-fable-5',
152 'claude-mythos-5',
153 'claude-fable-5-1',
154 'claude-mythos-5-1',
155 ],
156 page: 'tool-use/advisor-tool',
157 },
158 {
159 types: [
160 'tool_search_tool_regex_20251119',
161 'tool_search_tool_bm25_20251119',
162 ],
163 models: [
164 'claude-fable-5-1',
165 'claude-mythos-5-1',
166 'claude-fable-5',
167 'claude-mythos-5',
168 'claude-opus-5-5',
169 'claude-opus-5',
170 'claude-opus-4-8',
171 'claude-opus-4-7',
172 'claude-opus-4-6',
173 'claude-opus-4-5-20251101',
174 'claude-sonnet-4-6',
175 'claude-sonnet-4-5-20250929',
176 'claude-haiku-4-5-20251001',
177 ],
178 page: 'tool-use/tool-search-tool',
179 },
180 { types: ['mcp_toolset'], models: ANY, page: 'mcp-connector' },
181 // "available on all Claude 4 and later models"
182 { types: ['memory_20250818'], models: ANY, page: 'tool-use/memory-tool' },
183 { types: ['bash_20250124'], models: ANY, page: 'tool-use/bash-tool' },
184 // "`text_editor_20250728` is for Claude 4 and later models"
185 {
186 types: ['text_editor_20250728'],
187 models: ANY,
188 page: 'tool-use/text-editor-tool',
189 },
190 {
191 types: ['computer_toolset_20260801'],
192 models: [
193 'claude-fable-5-1',
194 'claude-mythos-5-1',
195 'claude-fable-5',
196 'claude-mythos-5',
197 'claude-opus-5-5',
198 'claude-opus-5',
199 'claude-sonnet-5',
200 'claude-opus-4-8',
201 ],
202 page: 'tool-use/computer-use-tool',
203 },
204 {
205 // Claude 5.5 and later reject it on the Claude API.
206 types: ['computer_20251124'],
207 models: [
208 'claude-fable-5-1',
209 'claude-fable-5',
210 'claude-opus-5',
211 'claude-sonnet-5',
212 'claude-opus-4-8',
213 'claude-opus-4-7',
214 'claude-opus-4-6',
215 'claude-sonnet-4-6',
216 'claude-opus-4-5-20251101',
217 ],
218 page: 'tool-use/computer-use-tool',
219 },
220 {
221 types: ['computer_20250124'],
222 models: ['claude-sonnet-4-5-20250929', 'claude-haiku-4-5-20251001'],
223 page: 'tool-use/computer-use-tool',
224 },
225 {
226 types: ['browser_toolset_20260801'],
227 models: [
228 'claude-fable-5-1',
229 'claude-mythos-5-1',
230 'claude-fable-5',
231 'claude-mythos-5',
232 'claude-opus-5-5',
233 'claude-opus-5',
234 'claude-sonnet-5',
235 'claude-opus-4-8',
236 ],
237 page: 'tool-use/browser-use-tool',
238 },
239]
240
241/** Tool type ids a Claude model accepts, each with its docs page. */
242export function anthropicServerTools(rawId: string): {
243 serverTools: Array<string>
244 sources: Record<string, FactSource>
245} {
246 const serverTools: Array<string> = []
247 const sources: Record<string, FactSource> = {}
248 for (const row of TOOLS) {
249 if (row.models && !row.models.includes(rawId)) continue
250 for (const type of row.types) {
251 serverTools.push(type)
252 sources[type] = {
253 derivation: 'docs-derived',
254 sourceUrl: `${TOOL_DOCS}/${row.page}`,
255 path: `serverTools.${type}`,
256 }
257 }
258 }
259 return { serverTools, sources }
260}
261
262const SAMPLING = ['temperature', 'top_p', 'top_k']
263const GUIDES = 'https://platform.claude.com/docs/en/models'
264
265// ponytail: hand-written from the migration guides on 2026-10-26. The guides
266// state sampling in prose, as a change to make when moving to a newer model,
267// so both lists are read from them rather than quoted: Fable 5, Opus 4.7/4.8,
268// Sonnet 5 and Mythos follow the guides' "Opus 4.7 and later" and carried-over
269// restriction wording. An id in neither list stays unstated, so re-verify on
270// each launch (new Claude models reject it).
271/** A 400 on Opus 4.7 and later and on every Claude 5 model. */
272const SAMPLING_REJECTED = [
273 'claude-fable-5-1',
274 'claude-fable-5',
275 'claude-mythos-5-1',
276 'claude-mythos-5',
277 'claude-opus-5-5',
278 'claude-opus-5',
279 'claude-opus-4-8',
280 'claude-opus-4-7',
281 'claude-sonnet-5-5',
282 'claude-sonnet-5',
283 'claude-haiku-5-5',
284]
285/** Models the guides tell the reader to strip sampling from when migrating. */
286const SAMPLING_ACCEPTED = [
287 'claude-opus-4-6',
288 'claude-opus-4-5-20251101',
289 'claude-opus-4-1-20250805',
290 'claude-opus-4-20250514',
291 'claude-sonnet-4-6',
292 'claude-sonnet-4-5-20250929',
293 'claude-sonnet-4-20250514',
294 'claude-haiku-4-5-20251001',
295]
296
297/** The family's own migration guide, where the removal is stated. */
298function samplingGuide(rawId: string): string {
299 const guide = rawId.includes('sonnet')
300 ? 'sonnet-5-5'
301 : rawId.includes('haiku')
302 ? 'haiku-5-5'
303 : rawId.includes('fable') || rawId.includes('mythos')
304 ? 'fable-5-1'
305 : 'opus-5-5'
306 return `${GUIDES}/${guide}/migration-guide`
307}
308
309/** Sampling flags the guides state for a model: `true` list, `false` list. */
310export function anthropicSampling(rawId: string): {
311 supported: Array<string>
312 unsupported: Array<string>
313 sources: Record<string, FactSource>
314} {
315 const rejected = SAMPLING_REJECTED.includes(rawId)
316 if (!rejected && !SAMPLING_ACCEPTED.includes(rawId)) {
317 return { supported: [], unsupported: [], sources: {} }
318 }
319 const source: FactSource = {
320 derivation: 'docs-derived',
321 sourceUrl: samplingGuide(rawId),
322 path: rejected ? 'sampling parameters' : 'migrating from earlier models',
323 }
324 return {
325 supported: rejected ? [] : SAMPLING,
326 unsupported: rejected ? SAMPLING : [],
327 sources: Object.fromEntries(SAMPLING.map((flag) => [flag, source])),
328 }
329}
330
331/** Reasoning + server tools for one listed model, with provenance. */
332export async function anthropicModelFeatures(
333 kv?: KVNamespace,
334 cached: typeof cachedDocs = cachedDocs,
335 fetcher: Fetcher = fetch,
336) {
337 const doc = await cached(kv, ANTHROPIC_THINKING_URL, async () => {
338 const markdown = await fetchText(ANTHROPIC_THINKING_URL, undefined, fetcher)
339 const parsed = parseThinkingTable(markdown)
340 assertParsed(parsed, 'anthropic thinking table')
341 return {
342 alwaysOn: Object.fromEntries(parsed),
343 hash: await sha256Text(markdown),
344 }
345 })
346 return (
347 rawId: string,
348 displayName: string | null | undefined,
349 caps: AnthropicThinkingCaps | undefined,
350 ) => {
351 const key = (displayName ?? '').trim().toLowerCase()
352 const listed = doc.alwaysOn[key]
353 const reasoning = anthropicReasoning(caps, listed ?? false)
354 const { serverTools, sources } = anthropicServerTools(rawId)
355 const factSources: ModelFactSources = { serverTools: sources }
356 if (reasoning) {
357 // Mode and efforts are the listing's; `mandatory` needs the docs row.
358 factSources.reasoning =
359 listed === undefined
360 ? { derivation: 'listing' }
361 : {
362 derivation: 'docs-derived',
363 sourceUrl: ANTHROPIC_THINKING_URL,
364 sourceHash: doc.hash,
365 path: key,
366 }
367 }
368 return { reasoning, serverTools, factSources }
369 }
370}