Optimize For Agent Calls
The one-sentence rule
Every server-side interaction — reading the key/token, auth, timeouts, polling for progress, fetching results — must live inside a single client script that the agent calls. The agent must only pass inputs and read one JSON result. If the agent is forced to curl / poll / read the secret by hand, the interface is under-scripted and should be fixed.
Why this is the central principle
An agent that has to:
cata secret file and paste it into anAuthorization:header,- hand-write a
curlper API endpoint, - loop-poll
/queueor/historyfor task progress, - decode download links and re-fetch the file to disk,
…burns 5–10 tool calls per job, every one of which can fail, hang, or leak the secret. The same work in one script = 1 call, 1 JSON return, no secret in context, and all the error handling lives in the code the agent can read.
This is not a "nice to have" — it is the biggest source of wasted agent turns on file/queue tasks.
The 4-layer template (copy the pattern, not the words)
A good agent-facing client script has these layers, inside-out:
agent ──call script──► [CLI] ──read config/secret──► [auth+timeout+errors]
└──────────── result JSON ──────────────► agent
- CLI layer — argparse subcommands. Each maps to one operation
(e.g.
add,status,list,delete,health). The agent only ever types the subcommand + its arguments. No raw HTTP verbs. - config + secret layer — loads address/defaults/token from a JSON config
file (see
filebed-config.json). The token is read at call time, never cached, never printed, never passed into the result. - transport layer — ONE place that sets headers (incl. the
Authorizationbearer), the timeout, and decodes non-JSON/HTTP-error responses into a clean JSON error object. If it exists, the agent nevercurls. - result layer — prints exactly ONE JSON object. Success =
{"ok": true, ...}(exit 0). Failure ={"ok": false, "error": "...", "hint": "..."}(exit 1). The agent reads one object and acts on it.
What "under-scripted" looks like (red flags)
If you see any of these in a skill, it is a design bug — move it into the script or delete the instruction:
- Agent reads a secret key —
cat .secret/ read env var / paste into a header. → script loads it; agent never touches it. - Agent runs
curl/ raw HTTP — the script should own every request. - Agent polls task progress — the script should contain the poll loop (submit → wait-for-completion → fetch result). A 150-line script doing submit→poll→fetch is cheaper than the agent driving that by hand for 8 calls.
- Agent "verifies the file exists" before calling — let the script validate
its own inputs (e.g.
os.path.isfile) and return a clean error. Data & Privacysection teaching the agent how to manage the endpoint — that instruction is what makes the agent go read keys and curl. Rephrase to "the script reads everything automatically; agent only passes inputs."
The 6 mistakes that waste agent turns (and the fixes)
These are the concrete lessons from turning filebed from curl→script:
-
Don't ask the agent to pre-validate inputs. A step like "confirm the file exists before calling
add" makes the agent runls/test -ffirst. Let the script validate and return{"ok": false, "error": "file not found"}. Fix: "just calladd; the script tells you if the path is bad." -
Don't leave the secret out of the script. If the skill tells the agent to find the token and put it in a header, the agent is doing the transport layer by hand. Fix:
load_secret()reads at call time; the value never appears in logs, results, or the agent's context. -
Give the agent a JSON contract, not a prose description. "Read the URL from the response" is weaker than showing the exact JSON. Fix: include a real success + failure JSON example in the SKILL.
-
Add human-readable fields for things the agent will quote back. If the agent is going to tell the user a size, duration, or count, return it in readable form too (e.g.
size_human, a friendly duration). The agent quotes it directly instead of formatting raw bytes. Fix: addsize_human,seconds_left, etc. -
Don't hand the agent a fallback the script already has. When the script already auto-polls, auto-fetches, and auto-substitutes on error, telling the agent "if it fails, check progress, then try again" just adds turns. Fix: say "one call covers submit→poll→fetch; if
ok:false, relay the error and fix the cause." -
Sync the SKILL.md 1:1 with the script. Any script capability not documented (or any instruction contradicting the script) sends the agent down the wrong path. Fix: rewrite the relevant SKILL sections whenever the script gains a subcommand or a field.
Delivery ordering lesson (from the filebed work)
Two separate concerns the SKILL got conflated, that now live in one system:
filebedis a fallback, not the primary channel. Send the file as a normal Telegram/WhatsApp attachment first; only fall back to a filebed link when the platform rejects it. This was encoded as adirect_send_recommendedflag (true when the file is ≤ 3 MiB) so the agent never guesses.- The secret and the URL are different threat models. The bearer token is the server's auth, sent only to localhost, never printed. The public URL is a shareable link. Password-protected links deliver the password over a separate channel. Keep those rules explicit in the SKILL.
How to verify a rewrite (do this before shipping)
- Syntax + import test —
python3 -c "import ast; ast.parse(open(path).read())". - End-to-end against the live service — health/subcommand, plus the failure paths (missing file, bad token, unreachable service). Confirm each returns the correct exit code and JSON shape.
- Secret hygiene check — grep the output of a successful run for the token; it must not appear. Confirm URLs never contain the path or the secret.
openclaw skills check— confirm the skill is still recognized and the new fields didn't break the frontmatter.- Cleanup — revoke/delete every test artifact (links, temp files) so the live service and disk stay clean.
Versioning
Bump package.json version when the script's contract changes:
- minor (+new subcommand, +new JSON field, +new config key) — e.g.
0.1 → 0.2 - patch (internal error handling only, no contract change) Never ship an update with an unchanged version if the agent-facing contract changed — the version is the agent's only contract hint.
Worked examples in this repo
filebed-delivery(filebed →filebed.py+filebed-config.json): curl→script, token-at-call-time, JSON contract,direct_send_recommendedsize_humanfields, SKILL rewritten 1:1. Its git history is the template.
image-with-comfyui: the script already does submit→poll→fetch internally; the remaining gap was the SKILL telling the agent to manage the endpoint and curl status by hand. Fix = make "call one subcommand, read the printed paths" an iron rule, and delete the "go read the key / curl status" instructions.
