Captcha solving for
LangChain agents
An agent that meets a challenge has nothing in its toolset that can answer it, so it stalls or hallucinates its way around. Give it a solver as a tool and the block becomes one more call.
import time
import requests
CAPTCHANO_KEY = "cn-YOUR_API_KEY"
HEADERS = {"Authorization": f"Bearer {CAPTCHANO_KEY}"}
def solve(task: dict) -> dict:
"""Block until solved; returns e.g. {"token": "...", "userAgent": "..."}."""
r = requests.post(
"https://api.captchano.com/v1/solve",
params={"wait": 60},
json=task,
headers=HEADERS,
timeout=75,
)
data = r.json()
# A solve can outlast the 60 s wait: poll the task until it finishes.
while data.get("status") in ("queued", "solving"):
time.sleep(5)
data = requests.get(
f"https://api.captchano.com/v1/task/{data['id']}",
headers=HEADERS,
timeout=30,
).json()
if data.get("status") != "ready":
raise RuntimeError(data.get("error") or data)
return data["solution"]
from langchain_core.tools import tool
@tool
def solve_captcha(kind: str, sitekey: str, url: str) -> str:
"""Call when a captcha blocks the task. kind is recaptcha_v2, recaptcha_v3
or turnstile. Returns the token to submit with the form."""
return solve({"type": kind, "sitekey": sitekey, "url": url})["token"]
# add solve_captcha to your agent's tools listWhy agents need this specifically
A scraper that hits a challenge fails loudly and you fix it. An agent fails quietly: it reasons about the blocked page, invents a plausible next step, and reports progress it never made. The failure is worse because it is silent. A typed solver tool turns an ambiguous dead end into a deterministic result the agent can branch on.
What the tool returns
- A token or cookie — depending on the challenge type. Session-graded systems return a cookie plus the header set used to earn it.
- The user agent used, so the agent can replay it on submission.
- The cost of the solve, so an agent operating under a budget can account for it rather than spending blind.
Keeping the loop bounded
Give the agent a solve budget per run. Every response reports its cost, so a supervisor can cut the loop when spend passes a threshold rather than discovering it on the invoice. At $0.00598 per solve a typical agent run costs a few cents or less, but an unbounded retry loop is still an unbounded retry loop.
Keep the tool call blocking
The examples call ?wait=60, so the tool returns a finished token in a single step. That matters more for agents than for scripts: a tool that returns "still solving" forces the model to reason about polling, which it does badly. If you need a longer ceiling, raise wait up to 120 seconds. Request shapes for every challenge type are in the API reference.
How the solve fits into LangChain
In LangChain the solver is a tool with a typed schema. The agent calls it when its browser tool reports a challenge.
- 01Define the tool
Wrap the API call with the @tool decorator and a clear docstring.
- 02Bind it to the agent
Add it to the tools list alongside your browser tools.
- 03Describe when to use it
The docstring tells the model to call it only when a captcha is present.
- 04Return a clear result
Return the token or a short error the model can act on.
Getting the token into the page
The tool returns the token as a string the agent passes to its browser or HTTP tool.
Where integrations usually go wrong
The model decides when to call a tool from its description. Say exactly what inputs it needs and what it returns.
Cap solves per run so a confused agent cannot loop on a page that keeps challenging it.
LangChain captcha FAQ
Does it work with LangGraph?
Yes. A LangChain tool is a node-callable function in LangGraph too, so the same definition works in both.
Which model works best with the tool?
Any model that supports tool calling. The tool's inputs are simple strings, so smaller models handle it fine.
How does the agent know a captcha is present?
Its browser or scraping tool should report it, for example by returning the sitekey when it finds one. Make that part of the tool's output.
Can I use it in an async agent?
Yes. Use an async HTTP client inside the tool and define it with async def.
Guides that help
Ready to build?
Start with a free 7-day trial of the Hobby plan, then pay per successful solve or keep the plan as your traffic grows. Cancel before the trial ends and you pay nothing.