Tool-calling tips
On this page
An agent that can call a paid API needs the same guardrails you'd give a new teammate: a budget per call, a clear rule for retries, and results small enough to read. These tips apply whether the agent uses the MCP server, tools generated from /openapi.json, or hand-written functions.
Cost controls to always set
Set these in your tool code, not in the model's prompt, so the model can't drop them.
| Control | Set it to | Why |
|---|---|---|
max_cost | A per-task ceiling, e.g. 0.002 for a SERP or page task, 0.05 for an llm_responses answer | No single task can cost more, upgrades included. It also lowers the wallet reservation. |
max_level | rendered for most page work | Keeps a page at $0.0011 or less. Allow protected ($0.003) only when the user asks for a hard-to-reach site. |
auto_upgrade | true (default) for interactive work; false for scheduled queues | false keeps queued SERP tasks at the queue price and fails at $0 rather than upgrading. |
depth | 10 unless the user asks for more | Organic SERPs bill per 10 results returned. |
| Units on per-item routes | photosLimit, resultsLimit, searchLimit | Photos, Meta ads and Yelp results bill per item. |
def serp_tool(keyword: str, location_code: int = 2840, language_code: str = "en") -> dict:
"""Top 10 Google results for one keyword. Costs at most $0.002."""
task = {
"keyword": keyword,
"location_code": location_code,
"language_code": language_code,
"depth": 10,
"max_cost": 0.002, # enforced here, not left to the model
}
body = requests.post(f"{BASE}/serp/google/organic/live/advanced", auth=AUTH, json=[task], timeout=120).json()
t = body["tasks"][0]
if t["status_code"] != 20000:
return {"error": t["status_code"], "message": t["status_message"], "cost": t["cost"]}
items = t["result"][0]["items"]
return {
"results": [{"rank": i["rank_absolute"], "title": i["title"], "url": i["url"]} for i in items],
"cost": t["cost"],
"fetched_at": t["result"][0]["fetched_at"],
"cache_hit": t["cache_hit"],
}Pick the mode for the agent's situation
- Answering a user now: live routes. Keep each call to one or a few tasks.
- Background research the user will read later:
task_postin the standard queue, thentasks_readyandtask_get. Tell the user it's running rather than guessing at results. - Never loop
task_gettightly. Poll every 30 seconds or more; reads count toward 120 requests per minute per key.
Handle errors without burning money
- Task errors are free.
40501names the bad field: let the model fix the argument and try once more. Don't retry it unchanged. 40210/ 402 means the wallet needs a top-up. Stop and tell the user; nothing was billed.40202/ 429: waitRetry-Afterseconds. Don't let the model spin.40402: the endpoint isn't served yet. Don't retry; checkGET /v1/account/support_matrix.- A live
POSTthat timed out may have run. Read it back (task_get,GET /v1/crawls/{id}) instead of calling again. There are no idempotency keys yet.
Keep results small
- Return only the fields the model needs (rank, title, URL, a snippet) plus
cost. Full SERP and page objects are large. - Cut page bodies. The MCP server cuts at 20,000 characters by default; do the same in your own tools.
- Keep
receipt_id,fetched_atandcache_hitin the result so the agent can cite where data came from and how fresh it is.
Tell the model what things cost
Put the price in each tool's description, from Pricing and billing. A model that knows a live SERP is $0.0018 and a queued one is $0.00054 makes better choices about batching and modes. GET /v1/account/user_data returns the current price table and balance if you'd rather load them at startup.
Use a dedicated key
Give each agent deployment its own key. Its rate limit is its own, and you can revoke it without touching anything else. Keep the key in your tool runtime; never put it in the prompt.