{"id":28709,"date":"2026-11-30T19:40:10","date_gmt":"2026-11-30T19:40:10","guid":{"rendered":"https:\/\/capskip.com\/?p=28709"},"modified":"2026-11-30T19:40:10","modified_gmt":"2026-11-30T19:40:10","slug":"ai-sdk-captcha-tool","status":"publish","type":"post","link":"https:\/\/capskip.com\/zh\/ai-sdk-captcha-tool\/","title":{"rendered":"AI SDK \u9a8c\u8bc1\u7801\u5de5\u5177\uff1a\u5728 Node.js \u4e2d\u7528 CapSkip \u8bc6\u522b reCAPTCHA"},"content":{"rendered":"<p>To build an AI SDK CAPTCHA tool, wrap the capskip package in tool() with a zod schema and keep the token out of the conversation. The tool returns an id and a length, the token waits in a Map that only your code reads, and your code posts it to the form after the loop ends. This guide builds that tool for AI SDK 7, drives it with the mock model from the AI SDK so no API key is needed, and measures what the loop does when a solve fails, hangs or repeats.<\/p>\n<h2 style=\"font-size:1.6rem;line-height:1.35;\">What you need<\/h2>\n<ul>\n<li>A Windows PC that runs CapSkip with reCAPTCHA solving enabled. The <a href=\"https:\/\/capskip.com\/setup-guide\/\">setup guide<\/a> covers installing it.<\/li>\n<li>Node.js 22 or later. The engines field of ai@7.0.181 and of its @ai-sdk packages says 22, and capskip 1.8.0 asks for 18. Every number below except the require result came from Windows 11 with Node.js 24.22.0 and 25.2.1, ai@7.0.181, capskip@1.8.0 and zod@4.6.7.<\/li>\n<li>zod@4.6.7 or later. The peer range of ai@7.0.181 is ^3.25.76 or ^4.1.8. Version 3.25.76 also worked, but npm audit flags it for a high severity memory advisory that 4.6.6 fixes.<\/li>\n<li>PowerShell for the commands, and a page of yours that shows a reCAPTCHA v2 checkbox.<\/li>\n<li>An address for CapSkip. Local mode listens on 127.0.0.1 for that PC only, and Server mode takes API calls from other machines too. The mode switch sits in the <a href=\"https:\/\/capskip.com\/setup-guide\/#connection-settings\">connection settings<\/a>.<\/li>\n<\/ul>\n<p>No language model was called. The model in every example is MockLanguageModelV4 from ai\/test, scripted to make the tool calls I tell it to. Nothing here shows how a real model behaves. A mock CapSkip and a mock sign-up site that I wrote stood in for the solver and the target, so every size and timing comes from mock runs with my own rules.<\/p>\n<h2 style=\"font-size:1.6rem;line-height:1.35;\">Step 1: install, then see what a returned token costs<\/h2>\n<div data-no-translation>\n<pre data-enlighter-language=\"bash\" class=\"EnlighterJSRAW\">mkdir ai-sdk-captcha\ncd ai-sdk-captcha\nnpm init -y\nnpm install ai zod capskip\nnpm ls --depth=0<\/pre>\n<\/div>\n<p>The AI SDK package is ES modules only, so every file here is .mjs. Loading it with require worked on Node.js 24.22.0 and 25.2.1 and failed with ERR_REQUIRE_ESM on 20.11.1. The capskip package is CommonJS, and a named import of CapSkip worked on all three.<\/p>\n<p>The obvious tool returns the token from execute, and the model then re-reads it on every later call. I compared three designs against the mock with a throwaway script that is not shown: a scripted model making three calls (solve, trivial tool, answer) and a mock token of 1,700 characters. The numbers are characters of prompt handed to the model, on both Node.js versions.<\/p>\n<table>\n<thead>\n<tr>\n<th>Tool design<\/th>\n<th>Call 1<\/th>\n<th>Call 2<\/th>\n<th>Call 3<\/th>\n<th>Total<\/th>\n<th>Token found in<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>execute returns the token<\/td>\n<td>57<\/td>\n<td>2,054<\/td>\n<td>2,282<\/td>\n<td>4,393<\/td>\n<td>Prompt, response messages, steps<\/td>\n<\/tr>\n<tr>\n<td>toModelOutput trims what the model sees<\/td>\n<td>57<\/td>\n<td>397<\/td>\n<td>625<\/td>\n<td>1,079<\/td>\n<td>The steps only<\/td>\n<\/tr>\n<tr>\n<td>The token goes into a Map<\/td>\n<td>57<\/td>\n<td>397<\/td>\n<td>625<\/td>\n<td>1,079<\/td>\n<td>Nowhere in the result<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>The first design copies the token into every later call, and into the response messages and result.steps, where histories and logs tend to be stored. In the second, the tool option toModelOutput maps the output to a smaller value for the model. The model saw an id and a length, and replaying the response messages did not bring the token back, but the full output stayed in result.steps, where onStepEnd callbacks and any code that prints the result can reach it. The third design hands the token to nothing the AI SDK builds, so this guide uses it.<\/p>\n<h2 style=\"font-size:1.6rem;line-height:1.35;\">Step 2: write the AI SDK CAPTCHA tool<\/h2>\n<p>Put the next three blocks into one file named captcha_tool.mjs. Set CAPSKIP_HOST, CAPSKIP_PORT and CAPSKIP_API_KEY only if CapSkip is not local with default settings. SOLVE_SECONDS is the capskip limit, 60 here. The setup guide gives the reCAPTCHA row 250 seconds, so a shorter limit makes this tool give up while CapSkip may still be solving.<\/p>\n<div data-no-translation>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\/\/ captcha_tool.mjs, part 1: the CapSkip client, the schema and the host check\nimport { tool } from &quot;ai&quot;;\nimport { z } from &quot;zod&quot;;\nimport {\n  CapSkip, ApiException, NetworkException, TimeoutException, ValidationException,\n} from &quot;capskip&quot;;\n\nconst solver = new CapSkip({\n  apiKey: process.env.CAPSKIP_API_KEY ?? &quot;capskip&quot;,\n  host: process.env.CAPSKIP_HOST ?? &quot;127.0.0.1&quot;,\n  port: Number(process.env.CAPSKIP_PORT ?? 8080),\n});\nconst SOLVE_SECONDS = Number(process.env.SOLVE_SECONDS ?? 60); \/\/ the SDK's own limit\nconst TOKEN_TTL_MS = 100_000; \/\/ a reCAPTCHA token lives about two minutes\n\n\/\/ What the model may send. strictObject refuses extra keys such as proxy.\nconst solveInput = z.strictObject({\n  sitekey: z.string().min(10).max(100).regex(\/^[A-Za-z0-9_-]+$\/)\n    .describe(&quot;The data-sitekey value of the reCAPTCHA widget&quot;),\n  pageUrl: z.string().max(2048)\n    .describe(&quot;Full address of the page that shows the widget&quot;),\n  version: z.enum([&quot;v2&quot;, &quot;v3&quot;]).default(&quot;v2&quot;),\n  invisible: z.boolean().optional().describe(&quot;v2 only&quot;),\n});\n\n\/\/ Checked inside the tool, so a prompt cannot talk the model around it.\n\/\/ The address that passes is the address that is sent: u.href, not the raw text.\nfunction checkedUrl(text, allowedHosts) {\n  try {\n    const u = new URL(text);\n    const ok = (u.protocol === &quot;http:&quot; || u.protocol === &quot;https:&quot;)\n      &amp;&amp; !u.username &amp;&amp; !u.password &amp;&amp; allowedHosts.has(u.host);\n    return ok ? u.href : null;\n  } catch {\n    return null;\n  }\n}<\/pre>\n<\/div>\n<p>The schema is the only validation the model gets, and the AI SDK enforces it. The zod rules reached the model call as JSON Schema keywords such as minLength and pattern, which a real provider may strip (untested). Arguments that break the schema do not make generateText throw. The SDK skips execute and sends the model an error text built from the zod issues. A plain z.object drops an unknown key such as proxy in silence, so the schema uses strictObject. The error text also repeats the value sent: a sitekey of 100,000 characters came back as 100,389 characters.<\/p>\n<p>The JSON Schema the model sees does not say that invisible belongs to version v2 only, so the capskip package refuses it before any request, with a ValidationException. An option present with the value undefined counts as present: on version v3, an invisible option set to undefined was refused. The tool adds it only when the model sent it.<\/p>\n<p>The model also picks the page address, and a page it has read can steer it elsewhere. The check lives inside the tool and sends the parsed address, not the raw text. I sent 19 odd addresses and one extra key through generateText against the mock:<\/p>\n<table>\n<thead>\n<tr>\n<th>Address from the model<\/th>\n<th>Result<\/th>\n<th>Jobs<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Another host, a lookalike prefix, user info in either order, a tab inside the host<\/td>\n<td>Refused by the tool<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>A file, javascript or ftp scheme, an empty string, plain text, a missing or different port, an IPv6 literal<\/td>\n<td>Refused by the tool<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>A backslash before @, spaces around, a line break plus a header, an upper case scheme, the IP in decimal<\/td>\n<td>Accepted: after parsing, the host is the allowed one<\/td>\n<td>1 each<\/td>\n<\/tr>\n<tr>\n<td>2,049 characters, or an extra key named proxy<\/td>\n<td>Refused by the schema<\/td>\n<td>0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>For the accepted rows the mock logged the cleaned address: the line break and header became a harmless path. Append the tool to the same file.<\/p>\n<div data-no-translation>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\/\/ captcha_tool.mjs, part 2: the tool (appended to part 1)\nconst fail = (error, retryable = false) =&gt; ({ ok: false, error, retryable });\n\n\/\/ The SDK has no signal option, so this stops the wait, not the solve.\nfunction untilAborted(promise, signal) {\n  if (!signal) return promise;\n  return Promise.race([\n    promise,\n    new Promise((_, reject) =&gt; {\n      if (signal.aborted) reject(signal.reason);\n      signal.addEventListener(&quot;abort&quot;, () =&gt; reject(signal.reason), { once: true });\n    }),\n  ]);\n}\n\n\/\/ One tool per run: the vault and the solve counter belong to that run.\nexport function createSolveTool({ vault, allowedHosts, maxSolves = 3 }) {\n  let used = 0;\n  let serial = 0;\n  return tool({\n    description:\n      &quot;Solve a reCAPTCHA on a page. The token stays with the application: &quot;\n      + &quot;you get an id and the token length. At most &quot; + maxSolves + &quot; solves per run.&quot;,\n    inputSchema: solveInput,\n    execute: async ({ sitekey, pageUrl, version, invisible }, { abortSignal }) =&gt; {\n      const url = checkedUrl(pageUrl, allowedHosts);\n      if (!url) return fail(&quot;host_not_allowed&quot;);\n      if (used &gt;= maxSolves) return fail(&quot;solve_budget_used_up&quot;);\n      used += 1; \/\/ every attempt counts, failed or not\n      const started = Date.now(); \/\/ the token is minted somewhere inside the solve\n      \/\/ An option set to undefined still counts as present on v3, so add it only when given.\n      const options = { version, timeout: SOLVE_SECONDS };\n      if (invisible !== undefined) options.invisible = invisible;\n      try {\n        const result = await untilAborted(solver.recaptcha(sitekey, url, options), abortSignal);\n        const id = &quot;cap_&quot; + ++serial;\n        vault.set(id, { token: result.code, born: started });\n        return { ok: true, id, tokenChars: result.code.length };\n      } catch (err) {\n        if (abortSignal?.aborted) return fail(&quot;stopped_waiting&quot;);\n        if (err instanceof TimeoutException) return fail(&quot;timeout&quot;, true);\n        if (err instanceof ValidationException) return fail(&quot;bad_arguments: &quot; + err.message);\n        if (err instanceof NetworkException) return fail(&quot;capskip_unreachable&quot;);\n        if (err instanceof ApiException) {\n          const unsolvable = err.message.includes(&quot;UNSOLVABLE&quot;);\n          return fail(unsolvable ? &quot;unsolvable&quot; : &quot;capskip_rejected&quot;, unsolvable);\n        }\n        throw err; \/\/ a bug in this file is not the model's problem\n      }\n    },\n  });\n}<\/pre>\n<\/div>\n<p>Every failure returns a small result with a name and a retryable flag (Step 4). Every attempt counts against maxSolves, failed ones included. The untilAborted helper stops waiting when the loop aborts, because the capskip package has no signal option. Append the last part, which your own code calls.<\/p>\n<div data-no-translation>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\/\/ captcha_tool.mjs, part 3: your code takes the token, once (appended to part 2)\nexport function takeToken(vault, id) {\n  const entry = vault.get(id);\n  vault.delete(id); \/\/ single use, even when it turns out to be too old\n  if (!entry || Date.now() - entry.born &gt; TOKEN_TTL_MS) return null;\n  return entry.token;\n}<\/pre>\n<\/div>\n<p>takeToken removes the entry whether or not it is fresh, so a token works once from your side too. The 100 second limit counts from the start of the solve and stays under the two minutes that <a href=\"https:\/\/developers.google.com\/recaptcha\/docs\/verify\" rel=\"noopener nofollow\" target=\"_blank\">Google documents<\/a> for a reCAPTCHA token. A mock that solved in 2 seconds gave a stored age of 3.81 seconds on return.<\/p>\n<h2 style=\"font-size:1.6rem;line-height:1.35;\">Step 3: run it with a mock model<\/h2>\n<p>Save this as mock_model.mjs.<\/p>\n<div data-no-translation>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\/\/ mock_model.mjs: a scripted model from the AI SDK's own test helpers, no API key\nimport { MockLanguageModelV4 } from &quot;ai\/test&quot;;\n\nconst usage = {\n  inputTokens: { total: 10, noCache: 10, cacheRead: undefined, cacheWrite: undefined },\n  outputTokens: { total: 5, text: 5, reasoning: undefined },\n};\n\n\/\/ One model turn that asks for a tool.\nexport const toolCall = (id, name, input) =&gt; ({\n  content: [{ type: &quot;tool-call&quot;, toolCallId: id, toolName: name, input: JSON.stringify(input) }],\n  finishReason: { unified: &quot;tool-calls&quot;, raw: undefined },\n  usage,\n  warnings: [],\n});\n\n\/\/ One model turn that answers in text and stops.\nexport const answer = (text) =&gt; ({\n  content: [{ type: &quot;text&quot;, text }],\n  finishReason: { unified: &quot;stop&quot;, raw: undefined },\n  usage,\n  warnings: [],\n});\n\n\/\/ Each argument is one turn, in order. The model never reads its prompt.\nexport const scriptedModel = (...turns) =&gt; new MockLanguageModelV4({ doGenerate: turns });<\/pre>\n<\/div>\n<p>Set the variables below, then save run_agent.mjs. The loop runs first. Then your code reads the id from the steps, takes the token from the Map and posts the form, so the model never sees the token or chooses where it goes.<\/p>\n<div data-no-translation>\n<pre data-enlighter-language=\"bash\" class=\"EnlighterJSRAW\"># PowerShell: set these once per window, before you run any file\n$env:PAGE_URL = &quot;https:\/\/your-site.example\/signup&quot;    # your own page\n$env:SIGNUP_URL = &quot;https:\/\/your-site.example\/signup&quot;  # where its form posts to\n$env:SITEKEY = &quot;YOUR_SITEKEY&quot;                         # its data-sitekey value<\/pre>\n<\/div>\n<div data-no-translation>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\/\/ run_agent.mjs: the loop first, then your own code spends the token\nimport { generateText, isStepCount } from &quot;ai&quot;;\nimport { createSolveTool, takeToken } from &quot;.\/captcha_tool.mjs&quot;;\nimport { scriptedModel, toolCall, answer } from &quot;.\/mock_model.mjs&quot;;\n\nconst PAGE_URL = process.env.PAGE_URL ?? &quot;https:\/\/example.com\/signup&quot;;\nconst SIGNUP_URL = process.env.SIGNUP_URL ?? PAGE_URL; \/\/ where your form posts to\nconst SITEKEY = process.env.SITEKEY ?? &quot;YOUR_SITEKEY&quot;;\n\n\/\/ Swap this for your provider's model to use a real one. This one never reads its prompt.\nconst model = scriptedModel(\n  toolCall(&quot;c1&quot;, &quot;solve_captcha&quot;, { sitekey: SITEKEY, pageUrl: PAGE_URL }),\n  answer(&quot;The CAPTCHA is solved.&quot;),\n);\n\nconst vault = new Map(); \/\/ this run's tokens, never sent to the model\nconst allowedHosts = new Set([new URL(PAGE_URL).host]); \/\/ set by you, not the model\n\nlet result;\ntry {\n  result = await generateText({\n    model,\n    tools: { solve_captcha: createSolveTool({ vault, allowedHosts, maxSolves: 3 }) },\n    instructions: &quot;Call solve_captcha, then answer in one sentence. If a result says &quot;\n      + &quot;retryable is false, stop and say why.&quot;,\n    prompt: `Solve the reCAPTCHA on ${PAGE_URL}. The sitekey is ${SITEKEY}.`,\n    stopWhen: isStepCount(5),\n    abortSignal: AbortSignal.timeout(240_000), \/\/ the whole call\n    timeout: { toolMs: 90_000 }, \/\/ each tool call, needs the tool to read abortSignal\n  });\n} catch (err) {\n  \/\/ AbortSignal.timeout raises a DOMException named TimeoutError, not CapSkip's class.\n  console.error(&quot;run stopped:&quot;, err.name, err.message);\n  process.exit(1);\n}\n\nconst solved = result.steps.flatMap((s) =&gt; s.toolResults).map((r) =&gt; r.output).findLast((o) =&gt; o.ok);\nif (!solved) {\n  console.log(&quot;no solve:&quot;, result.text);\n  process.exit(1);\n}\nconst token = takeToken(vault, solved.id);\nconst reply = await fetch(SIGNUP_URL, {\n  method: &quot;POST&quot;,\n  headers: { Referer: PAGE_URL }, \/\/ fetch sends none of its own\n  body: new URLSearchParams({ email: &quot;me@example.com&quot;, &quot;g-recaptcha-response&quot;: token }),\n  redirect: &quot;manual&quot;,\n});\nconsole.log(&quot;model said:&quot;, result.text);\nconsole.log(&quot;token chars:&quot;, token.length, &quot;| form answered&quot;, reply.status);<\/pre>\n<\/div>\n<p>The last file, hostile.mjs, imports the two modules above, sends refused addresses through the loop and needs no running CapSkip.<\/p>\n<div data-no-translation>\n<pre data-enlighter-language=\"js\" class=\"EnlighterJSRAW\">\/\/ hostile.mjs: addresses a page the model has read could push it toward\nimport { generateText, isStepCount } from &quot;ai&quot;;\nimport { createSolveTool } from &quot;.\/captcha_tool.mjs&quot;;\nimport { scriptedModel, toolCall, answer } from &quot;.\/mock_model.mjs&quot;;\n\nconst PAGE_URL = process.env.PAGE_URL ?? &quot;https:\/\/example.com\/signup&quot;;\nconst page = new URL(PAGE_URL);\nconst allowedHosts = new Set([page.host]);\nconst attacks = {\n  &quot;another host&quot;: &quot;https:\/\/evil.example\/signup&quot;,\n  &quot;allowed name as a prefix&quot;: `https:\/\/${page.host}.evil.example\/`,\n  &quot;user info&quot;: `https:\/\/${page.host}@evil.example\/`,\n  &quot;file scheme&quot;: &quot;file:\/\/\/C:\/Windows\/win.ini&quot;,\n  &quot;javascript scheme&quot;: &quot;javascript:alert(1)&quot;,\n  &quot;empty string&quot;: &quot;&quot;,\n  &quot;other port&quot;: `https:\/\/${page.hostname}:9999\/`,\n};\n\nfor (const [label, pageUrl] of Object.entries(attacks)) {\n  const model = scriptedModel(\n    toolCall(&quot;c1&quot;, &quot;solve_captcha&quot;, { sitekey: &quot;YOUR_SITEKEY&quot;, pageUrl }),\n    answer(&quot;done&quot;),\n  );\n  const result = await generateText({\n    model,\n    tools: { solve_captcha: createSolveTool({ vault: new Map(), allowedHosts }) },\n    prompt: &quot;go&quot;,\n    stopWhen: isStepCount(3),\n  });\n  const out = result.steps[0].toolResults[0].output;\n  console.log(label.padEnd(26), out.ok ? &quot;ACCEPTED&quot; : out.error);\n}<\/pre>\n<\/div>\n<div data-no-translation>\n<pre data-enlighter-language=\"bash\" class=\"EnlighterJSRAW\"># PowerShell, in the same folder\nnode hostile.mjs\nnode run_agent.mjs<\/pre>\n<\/div>\n<p>hostile.mjs prints host_not_allowed seven times. run_agent.mjs prints the model&#8217;s sentence, the token length and the form status: 1,700 and 200 against the mock. With the first task unsolvable, the scripted model still claimed success, but run_agent.mjs printed no solve and exited with 1: it trusts the steps, not the sentence.<\/p>\n<h2 style=\"font-size:1.6rem;line-height:1.35;\">Step 4: what the loop does when something goes wrong<\/h2>\n<h3 style=\"font-size:1.3rem;line-height:1.4;\">AI SDK tool error handling when the solve fails<\/h3>\n<p>An exception that escaped a tool ended the run in the <a href=\"https:\/\/capskip.com\/langchain-captcha-tool\/\">LangChain tool guide<\/a>. In AI SDK 7 it doesn&#8217;t: a thrown error becomes a tool-error part, the model reads its text and the loop goes on. I ran each failure with a bare tool and with the tool above, and all eight runs ended with the loop still running.<\/p>\n<table>\n<thead>\n<tr>\n<th>Failure in the mock run<\/th>\n<th>A bare tool throws, the model reads<\/th>\n<th>This tool returns, the model reads<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>The first task is unsolvable<\/td>\n<td>ApiException: ERROR_CAPTCHA_UNSOLVABLE<\/td>\n<td>Error unsolvable, retryable true<\/td>\n<\/tr>\n<tr>\n<td>Never ready, SDK limit 2 seconds<\/td>\n<td>TimeoutException: timeout 2 exceeded<\/td>\n<td>Error timeout, retryable true<\/td>\n<\/tr>\n<tr>\n<td>Nothing listens on the port<\/td>\n<td>NetworkException: connect ECONNREFUSED with 127.0.0.1 and the port you set<\/td>\n<td>Error capskip_unreachable<\/td>\n<\/tr>\n<tr>\n<td>Version v3 with invisible set<\/td>\n<td>ValidationException, with a sentence naming invisible and version v2<\/td>\n<td>Error bad_arguments, with that sentence<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>So the reason to return a result is control. A thrown error starts with a class name, carries no advice and, for a NetworkException, names the address of your CapSkip machine. The tool still throws for anything it doesn&#8217;t recognise.<\/p>\n<p>I also scripted a model that retries only when retryable is true. With the first task unsolvable it made 3 model calls and 2 jobs. With every task unsolvable it stopped after 5 calls and 3 jobs, on solve_budget_used_up. A disallowed address took 2 calls and no job. That shows the wiring, not that a real model obeys a flag.<\/p>\n<h3 style=\"font-size:1.3rem;line-height:1.4;\">stopWhen and isStepCount against a runaway agent<\/h3>\n<p>The loop is governed by stopWhen, described in the <a href=\"https:\/\/ai-sdk.dev\/docs\/agents\/loop-control\" rel=\"noopener nofollow\" target=\"_blank\">AI SDK loop control documentation<\/a>, and its default is one step. With the default, one requested solve ran, but the loop ended before the model read the result and result.text was empty. A step limit also doesn&#8217;t limit solves: one step can hold several tool calls, and the AI SDK runs them at once. Every row below used a mock that solves in 1.5 seconds.<\/p>\n<table>\n<thead>\n<tr>\n<th>Mock run<\/th>\n<th>Model calls<\/th>\n<th>Jobs<\/th>\n<th>Time<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Default stopWhen, one solve requested<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>1.8 s, text empty<\/td>\n<\/tr>\n<tr>\n<td>isStepCount(5), one solve requested<\/td>\n<td>2<\/td>\n<td>1<\/td>\n<td>1.8 s, text present<\/td>\n<\/tr>\n<tr>\n<td>isStepCount(8), model asks every step, maxSolves 100<\/td>\n<td>8<\/td>\n<td>8<\/td>\n<td>14.3 s<\/td>\n<\/tr>\n<tr>\n<td>The same, maxSolves 3<\/td>\n<td>8<\/td>\n<td>3<\/td>\n<td>5.4 s, five refused<\/td>\n<\/tr>\n<tr>\n<td>One step of five parallel calls, isStepCount(2), maxSolves 100<\/td>\n<td>2<\/td>\n<td>5<\/td>\n<td>1.8 s<\/td>\n<\/tr>\n<tr>\n<td>The same, maxSolves 3<\/td>\n<td>2<\/td>\n<td>3<\/td>\n<td>1.8 s<\/td>\n<\/tr>\n<tr>\n<td>The same, toolCallConcurrency 1<\/td>\n<td>2<\/td>\n<td>3<\/td>\n<td>5.4 s<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Keep both limits: isStepCount ends the loop and maxSolves ends the spending. Per the setup guide, solves of one type run in parallel only up to the Max. Threads setting of that type. Use toolCallConcurrency to hold the calls of one step to that number; a value of 1 tripled the time here.<\/p>\n<h3 style=\"font-size:1.3rem;line-height:1.4;\">AI SDK tool timeout and abortSignal through the loop<\/h3>\n<p>generateText takes an abortSignal and a timeout option (totalMs, stepMs, toolMs). In the source, the AI SDK merges the limit into the abortSignal it hands to execute and does nothing more, so a tool that ignores the signal isn&#8217;t stopped. I ran each setup against a mock that never answers, with the capskip limit at 4 seconds unless noted, on Node.js 24.22.0. Node.js 25.2.1 gave the same outcomes within 0.1 s.<\/p>\n<table>\n<thead>\n<tr>\n<th>Setup<\/th>\n<th>Tool ended after<\/th>\n<th>generateText<\/th>\n<th>The model read<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>toolMs 2000, the tool reads abortSignal<\/td>\n<td>2.01 s<\/td>\n<td>Returned at 2.02 s<\/td>\n<td>stopped_waiting<\/td>\n<\/tr>\n<tr>\n<td>toolMs 2000, a bare tool that ignores it<\/td>\n<td>7.82 s<\/td>\n<td>Returned at 7.83 s<\/td>\n<td>TimeoutException: timeout 4 exceeded<\/td>\n<\/tr>\n<tr>\n<td>abortSignal from AbortSignal.timeout(2000)<\/td>\n<td>2.00 s<\/td>\n<td>Threw at 2.01 s<\/td>\n<td>Nothing, no second model call<\/td>\n<\/tr>\n<tr>\n<td>timeout with totalMs 2000<\/td>\n<td>2.01 s<\/td>\n<td>Threw at 2.02 s<\/td>\n<td>Nothing, no second model call<\/td>\n<\/tr>\n<tr>\n<td>capskip limit 2 s, toolMs 9000<\/td>\n<td>3.80 s<\/td>\n<td>Returned at 3.81 s<\/td>\n<td>Error timeout, retryable true<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>toolMs works only because untilAborted reads the signal. The capskip package polls after 0.25, 0.75, 1.75, 3.75 and 7.75 seconds and then every 5, and checks its limit only between polls, so a limit of 2 returned at 3.80 seconds, a limit of 4 at 7.82 and the default 60 at 62.91, and it can run up to 5 seconds late. Keep that late exit below toolMs, and toolMs below the whole-call limit. A limit on the whole call gives the model no result: generateText throws a DOMException named TimeoutError, not the capskip TimeoutException, so catch it around the call, as run_agent.mjs does.<\/p>\n<p>A solve that untilAborted gave up on isn&#8217;t cancelled: the package polled once more at 3.80 seconds. A real CapSkip may keep the slot meanwhile, untested.<\/p>\n<h2 style=\"font-size:1.6rem;line-height:1.35;\">Common errors<\/h2>\n<table>\n<thead>\n<tr>\n<th>What you see<\/th>\n<th>Cause<\/th>\n<th>Fix<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>ERR_REQUIRE_ESM<\/td>\n<td>Node.js 20.11.1 loading the AI SDK with require<\/td>\n<td>Use .mjs files on Node.js 22 or later<\/td>\n<\/tr>\n<tr>\n<td>One model call, then empty result.text<\/td>\n<td>The default stopWhen is one step<\/td>\n<td>Pass isStepCount with room for an answer<\/td>\n<\/tr>\n<tr>\n<td>Error text starting AI_InvalidToolInputError<\/td>\n<td>The arguments broke the schema, execute never ran<\/td>\n<td>Nothing. A real model should correct itself, untested<\/td>\n<\/tr>\n<tr>\n<td>stopped_waiting<\/td>\n<td>toolMs or an abort came before the capskip limit<\/td>\n<td>Raise toolMs above the late exit of that limit<\/td>\n<\/tr>\n<tr>\n<td>TimeoutError thrown by generateText<\/td>\n<td>The whole-call limit ran out<\/td>\n<td>Catch it. It is not the capskip TimeoutException<\/td>\n<\/tr>\n<tr>\n<td>A tutorial using stepCountIs or system<\/td>\n<td>AI SDK 7 renamed them to isStepCount and instructions<\/td>\n<td>Use the new names. The old ones still ran here, with no warning printed<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 style=\"font-size:1.6rem;line-height:1.35;\">FAQ<\/h2>\n<details style=\"border:1px solid #e2e5ee;border-radius:10px;padding:14px 18px;margin:0 0 12px;\">\n<summary style=\"cursor:pointer;\">\n<h3 style=\"font-size:1.15rem;line-height:1.4;display:inline;margin:0;\">Does an AI SDK CAPTCHA tool work with a real model?<\/h3>\n<\/summary>\n<p style=\"margin:12px 0 0;\">The mock runs exercised the tool, the schema and the loop, which don&#8217;t depend on the model. How a given model fills the schema, corrects itself after an error text or reacts to a retryable flag is untested here.<\/p>\n<\/details>\n<details style=\"border:1px solid #e2e5ee;border-radius:10px;padding:14px 18px;margin:0 0 12px;\">\n<summary style=\"cursor:pointer;\">\n<h3 style=\"font-size:1.15rem;line-height:1.4;display:inline;margin:0;\">Can the agent and CapSkip run on different machines?<\/h3>\n<\/summary>\n<p style=\"margin:12px 0 0;\">Yes, through Server mode. Enable it in CapSkip, give that PC a static public IP when the agent sits outside your network, and set CAPSKIP_HOST, CAPSKIP_PORT and CAPSKIP_API_KEY where the agent runs. It stays your own Windows PC, and solves stay unmetered. Requests travel as plain HTTP, so keep the link on a private network or a VPN and turn API key validation on. In Server mode CapSkip allows inbound connections on its port from any address, so an allow rule of your own narrows nothing, while a Windows Firewall block rule for the addresses you distrust does. No second machine was tested.<\/p>\n<\/details>\n<details style=\"border:1px solid #e2e5ee;border-radius:10px;padding:14px 18px;margin:0 0 12px;\">\n<summary style=\"cursor:pointer;\">\n<h3 style=\"font-size:1.15rem;line-height:1.4;display:inline;margin:0;\">Do I need to write this tool for Claude Code?<\/h3>\n<\/summary>\n<p style=\"margin:12px 0 0;\">No. An MCP client such as Claude Code reaches CapSkip through its MCP server, so there is no tool to write, and the <a href=\"https:\/\/capskip.com\/mcp-captcha-solver\/\">MCP CAPTCHA solver page<\/a> describes that route. For Python agents, the LangChain guide linked in Step 4 and the <a href=\"https:\/\/capskip.com\/crewai-captcha-tool\/\">CrewAI tool guide<\/a> cover the same job.<\/p>\n<\/details>\n<h2 style=\"font-size:1.6rem;line-height:1.35;\">The short version<\/h2>\n<p>An AI SDK CAPTCHA tool keeps the token in a Map, returns an id and a length, refuses bad arguments with a strict schema, checks the address itself, returns named errors and sits behind isStepCount, maxSolves and a signal it reads. Under a flat license, a <a href=\"https:\/\/capskip.com\/\">local captcha solver<\/a> on your own Windows PC means an agent&#8217;s retries cost seconds, not fees. For other types, see the <a href=\"https:\/\/capskip.com\/nodejs-captcha-solver\/\">Node.js CAPTCHA solver page<\/a>. For a script with no agent, read the <a href=\"https:\/\/capskip.com\/recaptcha-v2-nodejs\/\">reCAPTCHA v2 in Node.js guide<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u4e2a\u7528 CapSkip \u8bc6\u522b reCAPTCHA\u3001\u53ea\u7ed9\u6a21\u578b\u4e00\u4e2a id \u548c\u4e00\u4e2a\u957f\u5ea6\u7684 AI SDK 7 \u5de5\u5177\u3002\u8fd9\u91cc\u8bb2 token \u5982\u4f55\u7559\u5728\u63d0\u793a\u4e4b\u5916\u3001\u5faa\u73af\u5982\u4f55\u5904\u7406\u9519\u8bef\uff0c\u4ee5\u53ca\u963b\u6b62\u5931\u63a7 agent \u7684\u4e09\u4e2a\u9650\u5236\u3002<\/p>","protected":false},"author":1,"featured_media":28708,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"AI SDK CAPTCHA Tool: reCAPTCHA in Node.js | CapSkip","rank_math_description":"Build an ai sdk captcha tool with CapSkip that keeps the token out of the prompt, caps solves per run and checks the page host. Copy the Node.js files.","rank_math_focus_keyword":"ai sdk captcha tool","footnotes":""},"categories":[70],"tags":[],"class_list":["post-28709","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-captcha"],"_links":{"self":[{"href":"https:\/\/capskip.com\/zh\/wp-json\/wp\/v2\/posts\/28709","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/capskip.com\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/capskip.com\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/capskip.com\/zh\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/capskip.com\/zh\/wp-json\/wp\/v2\/comments?post=28709"}],"version-history":[{"count":3,"href":"https:\/\/capskip.com\/zh\/wp-json\/wp\/v2\/posts\/28709\/revisions"}],"predecessor-version":[{"id":28779,"href":"https:\/\/capskip.com\/zh\/wp-json\/wp\/v2\/posts\/28709\/revisions\/28779"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/capskip.com\/zh\/wp-json\/wp\/v2\/media\/28708"}],"wp:attachment":[{"href":"https:\/\/capskip.com\/zh\/wp-json\/wp\/v2\/media?parent=28709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/capskip.com\/zh\/wp-json\/wp\/v2\/categories?post=28709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/capskip.com\/zh\/wp-json\/wp\/v2\/tags?post=28709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}