AI and automation
How do I give an AI agent a CAPTCHA tool?
Write one function that takes the challenge details, runs the create and poll cycle against the solving API, and returns the answer; then register it as a tool in your agent framework.
The shape of a good tool
Agents call tools by name with arguments that match a schema. A CAPTCHA tool should hide the whole create and poll loop behind one call: the model passes the kind of challenge, the page URL and the site key, and gets back the token or a clear error. Keep the schema small and the description precise, so the model knows when to call it and what to pass. Everything else, such as retries, timeouts and the API key, stays in your code where the model cannot see or change it.
A Python implementation
import os, time
import requests as http
SOLVER_BASE = "https://gatesolver.com"
TYPES = {"recaptcha_v2": "ReCaptchaV2TaskProxyLess", "recaptcha_v3": "ReCaptchaV3TaskProxyLess",
"turnstile": "AntiTurnstileTaskProxyLess"}
def solve_captcha(kind: str, page_url: str, site_key: str, action: str | None = None) -> str:
"""Return a token for the challenge on page_url, or a short error string."""
task = {"type": TYPES[kind], "websiteURL": page_url, "websiteKey": site_key}
if action and kind == "recaptcha_v3":
task["pageAction"] = action
secret = os.environ["GATESOLVER_KEY"]
opened = http.post(f"{SOLVER_BASE}/createTask", json={"clientKey": secret, "task": task}, timeout=20).json()
if opened.get("errorId"): # non-zero means the task was refused
return f"error: {opened.get('errorCode')}"
ask = {"clientKey": secret, "taskId": opened["taskId"]}
for _ in range(40): # 40 polls x 3 s = the 120 s task limit
time.sleep(3)
job = http.post(f"{SOLVER_BASE}/getTaskResult", json=ask, timeout=20).json()
if job.get("errorId"):
return f"error: {job.get('errorCode')}"
phase = job["status"]
if phase == "ready":
answer = job["solution"]
return answer.get("gRecaptchaResponse") or answer.get("token")
if phase == "failed":
return "error: task failed, try again"
return "error: timed out"
Returning short error strings instead of raising lets the model decide what to do next, such as retrying once or reporting the problem.
Registering it with frameworks
- LangChain: decorate the function with
@tool; the docstring and type hints become the schema. See the LangChain guide. - OpenAI Agents SDK: use the function tool decorator the SDK provides; the Agents SDK guide shows the current form.
- Browser agents: frameworks such as browser-use let you add custom actions that also receive the live page; see browser-use.
- MCP: you can expose the same function from a small Model Context Protocol server you write and run yourself, so any MCP-capable client can call it. GateSolver does not ship an MCP server; the MCP guide shows how to build your own in a few dozen lines.
Framework APIs change between releases, so check the current documentation of the version you use.
Who puts the token into the page?
Two designs work. In the first, the agent calls the tool, gets the token, and then calls a second tool such as "set field value" to place it. That is transparent but costs an extra step. In the second, the tool itself has access to the browser page and injects the token, then reports "solved and submitted". The second is faster and keeps long tokens out of the model's context, which saves tokens and avoids the model mangling a long string. Most production setups use it.
Guardrails
- Allowlist domains. Let the tool solve only on sites your task covers, so a prompt injection on some page cannot make the agent spend credit elsewhere.
- Cap calls per run. A loop in the agent's reasoning should hit a limit, not your balance.
- Keep the key out of the prompt. Read it from the environment inside the tool.
- Log every call. Task id, domain, outcome and cost let you audit what the agent did.
When the agent should call it
Describe the trigger in the tool description: "Use when a CAPTCHA, a 'verify you are human' check or a Turnstile widget blocks the task." Better still, detect challenges in your browser layer and call the tool automatically, so the model never wastes turns on a challenge page. The article on agents stuck on Turnstile shows that approach, and how LLMs call external APIs explains tool calling in general.
Common questions
Does GateSolver provide an MCP server?
No. You can write a small MCP server around the API yourself; the integration guide shows how.
Should the model see the API key?
No. Read it from the environment inside the tool so it never appears in prompts or logs.
Why return error strings instead of raising?
The model can read a short error and decide to retry or report, while an exception may end the run.
Which challenge types suit an agent tool?
Token types such as reCAPTCHA and Turnstile fit best, since the answer is one string to place in the page.
How do I stop an agent from overspending?
Allowlist domains, cap tool calls per run and log each call with its cost.
Keep reading
- MCP serverWrite your own small Python MCP server that gives any MCP client CAPTCHA solve and balance tools over the HTTP API.
- LangChainA typed LangChain StructuredTool around createTask and getTaskResult, with schema limits, tool errors the model understands, and agent wiring.
- Can AI agents handle CAPTCHAs on their own?Agents can spot a challenge and read the page, but a dependable pass needs a solving tool they call like any other.
- A LangChain agent that keeps hitting reCAPTCHAGive a LangChain agent a reCAPTCHA tool that spots the widget, requests a token over HTTP and continues the run.