360 Secure SkillHub
360 安全技能中心

optimize-for-agent-calls

一份技能设计方法论:主张把读密钥、鉴权、超时、轮询和取结果都封装进单个客户端脚本,让智能体只传输入、读一个 JSON 结果;给出四层模板、常见反模式与修复、交付顺序和重写验证步骤。

分类:开发
sunshinejnjn
sunshinejnjn
热度 0.00
最近更新 2026-10-27 04:27:12

安装

发给 Agent
帮我安装这个技能:npx secure-skills-cli add sunshinejnjn/optimize-for-agent-calls
兼容性
通用兼容
联网要求
不联网
数据出网
数据不出本机
Metadata
name
optimize-for-agent-calls
description
Methodology for turning a "raw API + curl" interface into an agent-friendly one. Use whenever an agent is repeatedly reading keys/tokens, building curl chains, polling for progress, or fetching results by hand — that is the signal the work should be moved INTO a single self-contained client script. Apply to the skill's own Python/CLI and its SKILL.md instructions together.
id
optimize-for-agent-calls
tags
["methodology","skill-design","agent-efficiency","scripting","api-client"]

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:

  • cat a secret file and paste it into an Authorization: header,
  • hand-write a curl per API endpoint,
  • loop-poll /queue or /history for 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
  1. 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.
  2. 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.
  3. transport layer — ONE place that sets headers (incl. the Authorization bearer), the timeout, and decodes non-JSON/HTTP-error responses into a clean JSON error object. If it exists, the agent never curls.
  4. 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 & Privacy section 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:

  1. Don't ask the agent to pre-validate inputs. A step like "confirm the file exists before calling add" makes the agent run ls/test -f first. Let the script validate and return {"ok": false, "error": "file not found"}. Fix: "just call add; the script tells you if the path is bad."

  2. 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.

  3. 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.

  4. 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: add size_human, seconds_left, etc.

  5. 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."

  6. 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:

  • filebed is 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 a direct_send_recommended flag (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)

  1. Syntax + import test — python3 -c "import ast; ast.parse(open(path).read())".
  2. 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.
  3. 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.
  4. openclaw skills check — confirm the skill is still recognized and the new fields didn't break the frontmatter.
  5. 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_recommended
    • size_human fields, 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.