Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

anthropic-features.ts

296 lines8.5 KB

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'
16​
17export const ANTHROPIC_THINKING_URL =
18 'https://platform.claude.com/docs/en/build-with-claude/thinking-troubleshooting.md'
19​
20const TOOL_DOCS = 'https://platform.claude.com/docs/en/agents-and-tools'
21​
22/** Display name (lowercased) → thinking cannot be turned off. */
23export function parseThinkingTable(markdown: string): Map<string, boolean> {
24 const out = new Map<string, boolean>()
25 for (const [model = '', types, defaultCell] of markdownTableRows(markdown)) {
26 if (!model.startsWith('Claude ') || !types || !defaultCell) continue
27 out.set(model.toLowerCase(), defaultCell.trim() === 'Always on')
28 }
29 return out
31​
32interface Supported {
33 supported?: boolean
35​
36export interface AnthropicThinkingCaps {
37 thinking?: Supported & {
38 types?: { enabled?: Supported; adaptive?: Supported }
39 }
40 effort?: Supported & Record<string, unknown>
42​
43/**
44 * `adaptive` wins when both are accepted: extended (`budget_tokens`) is
45 * deprecated on the models that still take it.
46 */
47export function anthropicReasoning(
48 caps: AnthropicThinkingCaps | undefined,
49 alwaysOn: boolean,
50): ModelReasoning | null {
51 const types = caps?.thinking?.types
52 if (!caps?.thinking?.supported || !types) return null
53 const mode = types.adaptive?.supported
54 ? 'adaptive'
55 : types.enabled?.supported
56 ? 'budget'
57 : null
58 if (!mode) return null
59 const efforts = Object.entries(caps.effort ?? {})
60 .filter(
61 ([, value]) =>
62 typeof value === 'object' &&
63 value !== null &&
64 (value as Supported).supported === true,
65 )
66 .map(([level]) => level)
67 return {
68 mode,
69 mandatory: alwaysOn,
70 ...(efforts.length > 0 ? { efforts } : {}),
71 }
73​
74/** Every listed model (the tool reference names no model limit). */
75const ANY = null
76​
77// ponytail: hand-written from the tool pages on 2026-09-25. New models get only the
78// unrestricted types until a row names them; re-verify on each launch.
79const TOOLS: Array<{
80 types: Array<string>
81 models: Array<string> | null
82 page: string
83}> = [
84 {
85 types: [
86 'web_search_20250305',
87 'web_search_20260209',
88 'web_search_20260318',
89 ],
90 models: ANY,
91 page: 'tool-use/web-search-tool',
92 },
93 {
94 types: ['web_fetch_20250910'],
95 models: ANY,
96 page: 'tool-use/web-fetch-tool',
97 },
98 {
99 // "Dynamic filtering is available with Claude Fable 5.1, …"
100 types: ['web_fetch_20260209', 'web_fetch_20260309', 'web_fetch_20260318'],
101 models: [
102 'claude-fable-5-1',
103 'claude-mythos-5-1',
104 'claude-fable-5',
105 'claude-mythos-5',
106 'claude-opus-4-8',
107 'claude-opus-4-7',
108 'claude-opus-4-6',
109 'claude-sonnet-5',
110 'claude-sonnet-4-6',
111 ],
112 page: 'tool-use/web-fetch-tool',
113 },
114 {
115 // "Every supported model accepts all three tool versions."
116 types: [
117 'code_execution_20250825',
118 'code_execution_20260120',
119 'code_execution_20260521',
120 ],
121 models: [
122 'claude-fable-5-1',
123 'claude-mythos-5-1',
124 'claude-fable-5',
125 'claude-mythos-5',
126 'claude-opus-5-5',
127 'claude-opus-5',
128 'claude-opus-4-8',
129 'claude-opus-4-7',
130 'claude-opus-4-6',
131 'claude-opus-4-5-20251101',
132 'claude-sonnet-5',
133 'claude-sonnet-4-6',
134 'claude-sonnet-4-5-20250929',
135 'claude-haiku-4-5-20251001',
136 ],
137 page: 'tool-use/code-execution-tool',
138 },
139 {
140 // Executors in the model-compatibility table.
141 types: ['advisor_20260301'],
142 models: [
143 'claude-haiku-4-5-20251001',
144 'claude-sonnet-4-6',
145 'claude-sonnet-5',
146 'claude-opus-4-6',
147 'claude-opus-4-7',
148 'claude-opus-4-8',
149 'claude-opus-5',
150 'claude-fable-5',
151 'claude-mythos-5',
152 'claude-fable-5-1',
153 'claude-mythos-5-1',
154 ],
155 page: 'tool-use/advisor-tool',
156 },
157 {
158 types: [
159 'tool_search_tool_regex_20251119',
160 'tool_search_tool_bm25_20251119',
161 ],
162 models: [
163 'claude-fable-5-1',
164 'claude-mythos-5-1',
165 'claude-fable-5',
166 'claude-mythos-5',
167 'claude-opus-5-5',
168 'claude-opus-5',
169 'claude-opus-4-8',
170 'claude-opus-4-7',
171 'claude-opus-4-6',
172 'claude-opus-4-5-20251101',
173 'claude-sonnet-4-6',
174 'claude-sonnet-4-5-20250929',
175 'claude-haiku-4-5-20251001',
176 ],
177 page: 'tool-use/tool-search-tool',
178 },
179 { types: ['mcp_toolset'], models: ANY, page: 'mcp-connector' },
180 // "available on all Claude 4 and later models"
181 { types: ['memory_20250818'], models: ANY, page: 'tool-use/memory-tool' },
182 { types: ['bash_20250124'], models: ANY, page: 'tool-use/bash-tool' },
183 // "`text_editor_20250728` is for Claude 4 and later models"
184 {
185 types: ['text_editor_20250728'],
186 models: ANY,
187 page: 'tool-use/text-editor-tool',
188 },
189 {
190 types: ['computer_toolset_20260801'],
191 models: [
192 'claude-fable-5-1',
193 'claude-mythos-5-1',
194 'claude-fable-5',
195 'claude-mythos-5',
196 'claude-opus-5-5',
197 'claude-opus-5',
198 'claude-sonnet-5',
199 'claude-opus-4-8',
200 ],
201 page: 'tool-use/computer-use-tool',
202 },
203 {
204 // Claude 5.5 and later reject it on the Claude API.
205 types: ['computer_20251124'],
206 models: [
207 'claude-fable-5-1',
208 'claude-fable-5',
209 'claude-opus-5',
210 'claude-sonnet-5',
211 'claude-opus-4-8',
212 'claude-opus-4-7',
213 'claude-opus-4-6',
214 'claude-sonnet-4-6',
215 'claude-opus-4-5-20251101',
216 ],
217 page: 'tool-use/computer-use-tool',
218 },
219 {
220 types: ['computer_20250124'],
221 models: ['claude-sonnet-4-5-20250929', 'claude-haiku-4-5-20251001'],
222 page: 'tool-use/computer-use-tool',
223 },
224 {
225 types: ['browser_toolset_20260801'],
226 models: [
227 'claude-fable-5-1',
228 'claude-mythos-5-1',
229 'claude-fable-5',
230 'claude-mythos-5',
231 'claude-opus-5-5',
232 'claude-opus-5',
233 'claude-sonnet-5',
234 'claude-opus-4-8',
235 ],
236 page: 'tool-use/browser-use-tool',
237 },
239​
240/** Tool type ids a Claude model accepts, each with its docs page. */
241export function anthropicServerTools(rawId: string): {
242 serverTools: Array<string>
243 sources: Record<string, FactSource>
244} {
245 const serverTools: Array<string> = []
246 const sources: Record<string, FactSource> = {}
247 for (const row of TOOLS) {
248 if (row.models && !row.models.includes(rawId)) continue
249 for (const type of row.types) {
250 serverTools.push(type)
251 sources[type] = {
252 derivation: 'docs-derived',
253 sourceUrl: `${TOOL_DOCS}/${row.page}`,
254 path: `serverTools.${type}`,
255 }
256 }
257 }
258 return { serverTools, sources }
260​
261/** Reasoning + server tools for one listed model, with provenance. */
262export async function anthropicModelFeatures(kv?: KVNamespace) {
263 const doc = await cachedDocs(kv, ANTHROPIC_THINKING_URL, async () => {
264 const markdown = await fetchText(ANTHROPIC_THINKING_URL)
265 const parsed = parseThinkingTable(markdown)
266 assertParsed(parsed, 'anthropic thinking table')
267 return {
268 alwaysOn: Object.fromEntries(parsed),
269 hash: await sha256Text(markdown),
270 }
271 })
272 return (
273 rawId: string,
274 displayName: string | null | undefined,
275 caps: AnthropicThinkingCaps | undefined,
276 ) => {
277 const key = (displayName ?? '').trim().toLowerCase()
278 const listed = doc.alwaysOn[key]
279 const reasoning = anthropicReasoning(caps, listed ?? false)
280 const { serverTools, sources } = anthropicServerTools(rawId)
281 const factSources: ModelFactSources = { serverTools: sources }
282 if (reasoning) {
283 // Mode and efforts are the listing's; `mandatory` needs the docs row.
284 factSources.reasoning =
285 listed === undefined
286 ? { derivation: 'listing' }
287 : {
288 derivation: 'docs-derived',
289 sourceUrl: ANTHROPIC_THINKING_URL,
290 sourceHash: doc.hash,
291 path: key,
292 }
293 }
294 return { reasoning, serverTools, factSources }
295 }