DuckDuckGo Search Rate Limited in Your AI Agent? Add a Free Fallback
A lot of Python agents get their web search from DuckDuckGo through the ddgs package (formerly duckduckgo_search). It needs no API key, which makes it a common choice in tutorials and starter agents. It also scrapes DuckDuckGo's HTML pages, so when DuckDuckGo decides your traffic looks automated, your agent's search tool stops returning anything. You see RatelimitException: ... 202 Ratelimit, a DDGSException: No results found., or an "anomaly" challenge page from html.duckduckgo.com. It's most likely on cloud servers, CI runners and shared IPs, or when an agent fires several searches in a row. Our cloud test server was blocked on the first try.
You don't have to give up DuckDuckGo to fix it. Keep it as the first choice and add a second search source that only runs when DuckDuckGo fails. This post shows that pattern in about 30 lines of Python, with AgentSearch as the fallback: no API key, no signup and no wallet, 25 free searches per IP in total, then pay per call. The 25 is a lifetime allowance, not a daily one, and it never resets.
What we actually ran (November 29, 2026): ddgs 9.16.1 and langchain-agentsearch 0.8.2 on Python 3.13, on a cloud server. DuckDuckGo blocked that server: a direct request to html.duckduckgo.com got HTTP 202 with an "anomaly" challenge page, and the ddgs call failed with DDGSException: No results found. The fallback ran and returned three real results from AgentSearch's free tier. We also forced a RatelimitException with the test below (one more real free search), and tested the other branches (timeout, empty list, DuckDuckGo succeeding, AgentSearch returning an error) offline with mocks. In total we used three real free AgentSearch searches for this post and no paid calls.
What a DuckDuckGo block looks like in Python
Which error you get depends on the package version:
| Package | What a block usually looks like |
|---|---|
ddgs 9.x (current) |
DDGSException: No results found. (sometimes TimeoutException) |
duckduckgo_search (old name, last release 8.1.1) |
RatelimitException: <url> 202 Ratelimit |
The old duckduckgo_search package raised RatelimitException when DuckDuckGo answered with status 202, 301, 400, 403, 418 or 429. Its PyPI page now says it has been renamed to ddgs. In ddgs 9.16.1, RatelimitException still exists, but we read the source and nothing raises it: a non-200 answer counts as "no results" from that engine, and when no engine returned anything, DDGS().text() raises DDGSException("No results found."). So code that only catches RatelimitException never sees the block on current versions. Catch DDGSException, the base class. RatelimitException and TimeoutException are subclasses of it, so one except covers all three.
Two more things to know about ddgs 9.x:
- The default
backend="auto"is a metasearch across several engines, not DuckDuckGo alone. If you want DuckDuckGo specifically, passbackend="duckduckgo", as the code below does. - Treat an empty result list as a failure too, so the fallback also covers wrappers that swallow the error and return nothing.
The fallback pattern
Install both packages:
pip install -U ddgs langchain-agentsearch
langchain-agentsearch is AgentSearch's LangChain package, but its tool is a plain object you call with .invoke(), so this works in any Python code, with or without LangChain:
# search_with_fallback.py: DuckDuckGo first, AgentSearch when DuckDuckGo blocks you
import json
from ddgs import DDGS
from ddgs.exceptions import DDGSException # RatelimitException and TimeoutException subclass it
from langchain_agentsearch import AgentSearchTool
fallback = AgentSearchTool(mode="free") # free tier: no key, no wallet
def web_search(query: str, max_results: int = 5) -> list[dict]:
try:
hits = DDGS().text(query, max_results=max_results, backend="duckduckgo")
if hits:
return [
{"title": h["title"], "url": h["href"], "snippet": h["body"], "source": "duckduckgo"}
for h in hits
]
except DDGSException as exc: # rate limit, block page, timeout or "No results found."
print(f"DuckDuckGo failed ({type(exc).__name__}: {exc}), using AgentSearch")
raw = fallback.invoke({"query": query, "max_results": min(max_results, 5)})
if raw.startswith("Error"): # the tool returns an "Error: ..." string instead of raising
raise RuntimeError(raw)
return [
{"title": r["title"], "url": r["url"], "snippet": r.get("content", ""), "source": "agentsearch"}
for r in json.loads(raw)["results"]
]
if __name__ == "__main__":
for hit in web_search("python asyncio TaskGroup docs", max_results=3):
print(hit["source"], "|", hit["title"], "|", hit["url"])
What it does:
- Tries DuckDuckGo and returns its results when there are any.
- On any
ddgserror, or an empty result list, it calls AgentSearch's web search with the same query. AgentSearch returns up to 5 results per call, somax_resultsis capped at 5 there. - Both branches return the same shape (
title,url,snippet, plussource), so the rest of your agent doesn't care which one answered.
mode="free" pins the AgentSearch tool to the free tier. That matters because in the package's default mode="auto", search pays per call when a wallet is configured. With mode="free", it stays free even if you add a wallet later for other tools.
Here's our run from the blocked server:
$ python search_with_fallback.py
DuckDuckGo failed (DDGSException: No results found.), using AgentSearch
agentsearch | Coroutines and tasks | https://docs.python.org/3/library/asyncio-task.html
agentsearch | Python asyncio TaskGroup by example - Stephen Gilmore | https://sglmr.com/blog/python-asyncio-taskgroup-by-example/
agentsearch | asyncio — Asynchronous I/O | https://docs.python.org/3/library/asyncio.html
The raw AgentSearch result also says which tier answered. Ours had "agentsearch_mode": "free: AgentSearch free tier, 25 free searches per IP in total, then pay per call (no wallet configured)".
Test the fallback without waiting for a block
On your laptop DuckDuckGo may work fine, so force the error to check the fallback branch. One gotcha we hit: ddgs.DDGS is a lazy proxy object, so mock.patch.object(DDGS, "text", ...) has no effect and the real search still runs. Patch the DDGS name inside your own module instead:
# test_fallback.py
from unittest import mock
from ddgs.exceptions import RatelimitException
import search_with_fallback as s
with mock.patch.object(s, "DDGS") as fake:
fake.return_value.text.side_effect = RatelimitException("202 Ratelimit")
for hit in s.web_search("python asyncio TaskGroup docs", max_results=3):
print(hit["source"], hit["url"])
This makes one real AgentSearch call, which counts against your free searches. To test without any network at all, also patch AgentSearchTool.invoke to return a canned JSON string, as we did for the timeout, empty-list and error cases.
Wire it into your agent
The function above is a plain Python function, so most frameworks can take it as a tool directly. If your framework already has a ready-made DuckDuckGo tool, you can also swap in an AgentSearch tool or keep both:
- LangChain / LangGraph:
from langchain_agentsearch import AgentSearchTooland passAgentSearchTool(mode="free")in your tools list, next to or instead ofDuckDuckGoSearchRun. See how to add web search to a LangChain agent and search the web, then read the page. - CrewAI:
pip install crewai-agentsearch, thenfrom crewai_agentsearch import AgentSearchToolandAgentSearchTool(mode="free"). See web search in a CrewAI agent. - smolagents: smolagents ships a
DuckDuckGoSearchTool. For AgentSearch, load its MCP server withMCPClient, as in web search in a smolagents agent, or wrap theweb_searchfunction above with smolagents'@tooldecorator. - Anything that speaks MCP: add the free hosted MCP server, nothing to install:
https://relay.agentsearchhq.com/mcp
It speaks MCP Streamable HTTP, needs no sign-in and has the same free allowance. Details and curl tests are in the free remote MCP server post.
If you'd rather let the model decide, give it both tools and say in the system prompt: use DuckDuckGo search first, and if it returns an error or nothing, use the AgentSearch search tool. The code fallback above is more predictable, because the switch never depends on the model noticing the error.
What the free tier covers, and what comes after
AgentSearch's free search is 25 searches per IP in total, then pay per call, with no key, account or wallet (shared daily capacity; paid calls have no cap). As a fallback that only runs when DuckDuckGo fails, it can stretch a long way, but it is a fixed allowance per IP address and it doesn't reset. On a shared server, other users of the same IP draw from the same 25. Once they're used, the tool answers with an Error: ... string, which the code above turns into a RuntimeError so you notice.
For a fallback that keeps working past that, AgentSearch's direct x402 API at api.agentsearchhq.com charges per call in USDC on Base, with no account, no API key and no minimum: search $0.003, news $0.003, extract $0.002 and search-and-read $0.005 (live prices on November 29, 2026). You're only charged when a usable answer comes back. In langchain-agentsearch, install the [paid] extra, configure a wallet and use mode="auto" or mode="paid". The LangChain search-then-read post walks through the wallet setup and spend cap, the x402 docs cover the raw API, and pricing lists every service. Use a new, dedicated wallet with a few dollars of USDC, never your main one.
Tips for fewer DuckDuckGo blocks
- Don't retry in a tight loop. Hammering DuckDuckGo after a block tends to keep you blocked. Fail over once, and come back to DuckDuckGo on the next query.
- Cache repeated queries. Agents often search the same thing more than once in a run, and a small in-process cache cuts the number of requests.
- Limit searches per task. A cap on searches per agent run keeps a confused model from spending your free allowance on both services.
- Keep the results untrusted. Snippets and pages can contain text written to steer an AI. Treat search results as data, not instructions, whichever service they came from.
FAQ
Why does DuckDuckGo search fail with RatelimitException or 202 Ratelimit?
The ddgs and duckduckgo_search packages fetch DuckDuckGo's HTML results pages, and DuckDuckGo can answer automated-looking traffic with a 202 status or a challenge page instead of results. The old duckduckgo_search package raised RatelimitException with "202 Ratelimit" for that. Current ddgs 9.x usually raises DDGSException: No results found. instead.
Which exception should I catch in ddgs?
Catch ddgs.exceptions.DDGSException. It's the base class, so it also covers RatelimitException and TimeoutException, and it's what ddgs 9.16.1 raises when every engine came back empty, which is how a block usually shows up. Also treat an empty result list as a failure.
Does the AgentSearch fallback need an API key?
No. AgentSearchTool(mode="free") from langchain-agentsearch and the hosted MCP server both work with no key, account or wallet, for 25 free searches per IP in total, then pay per call.
Do I have to replace DuckDuckGo?
No. The pattern keeps DuckDuckGo as the first choice and only calls AgentSearch when DuckDuckGo raises an error or returns nothing, so your free AgentSearch searches only go to queries DuckDuckGo couldn't answer.
What happens when the 25 free searches are used up?
The tool returns an error message instead of results, and the example code raises a RuntimeError. To keep the fallback working, pay per call through the direct x402 API: $0.003 per search in USDC on Base, no account and no API key.
Next steps
- Error handling for AI agent web tools: retryable errors, timeouts and partial pages.
- How to add web search to a LangChain agent.
- How to add web search to a CrewAI agent.
- Free remote MCP server for web search.
- All clients on the AgentSearch MCP page.
Keep DuckDuckGo first, catch DDGSException, and fall back to AgentSearchTool(mode="free"). Your agent keeps searching when DuckDuckGo blocks it: 25 free searches per IP in total with no key, then $0.003 per paid search.
More from the blog
- Error Handling for AI Agent Web Tools: Retryable Errors, Timeouts and Partial Pages
- How to Add Web Search to a LangChain Agent in Python (No API Key)
- How to Add Web Search to a CrewAI Agent (Free, No API Key)
- Free Remote MCP Server for Web Search: Hosted, No Install, No API Key
Service pages: Web Search API · Web Extract API · Search + Read API · Web Render API