Gopi Gugan 如何構建可呼叫真實工具而不失控的 AI 智能體
How I Build an AI Agent That Calls Real Tools — Without Letting It Run Wild - By Gopi Gugan
Gopi Gugan分享以程式碼護欄構建可呼叫真實工具的 AI 智能體,讓工具與執行流程由開發者控制。示例以白名單、參數結構驗證及 10 步上限限制工具呼叫,寫入操作須明確列入允許清單,破壞性操作則要經人工確認。文章亦建議記錄並定期檢視每次工具呼叫的追蹤資料,再用真實任務測試調整工具描述和規則。
I'm Gopi Gugan, a software engineer building AI features with FastAPI, Postgres/pgvector, and React/TypeScript. You can find more of my work at https://www.gopigugan.ca/. Lately I have been building an AI agent — not a chatbot that answers questions, but an agent that actually does things: it looks up orders, updates records, and calls real tools on my behalf. The first version scared me a little, and it should have. An agent that can act is an agent that can make a mess, so I rebuilt it with guardrails before I trusted it with anything real.
An agent is a loop, not a prompt
The mental model that helped me most is simple: an agent is a loop. It thinks, it picks a tool, it sees the result, and it decides what to do next. The model is only one part of that loop — the rest is code I control.
-
The tools: small, typed functions like
get_order(id)orupdate_status(id, status)— nothing the agent invents on the fly - The loop: a plain Python loop with a hard cap on steps, so it cannot run forever
- The transcript: every thought, tool call, and result is logged, so I can replay exactly what happened
I keep the tools narrow on purpose. An agent with one well-described tool is safer than an agent with twenty vague ones. If a tool can delete something, I think twice about whether the agent needs it at all — most of the time it doesn't.
Give the agent a whitelist, not a warning
My first version told the model "be careful" in the system prompt. That is not a control, it is a hope. What actually works is enforcing the rules in code, where the model cannot talk its way around them.
- Only tools I registered can be called — the model cannot invent a new one
- Write actions need an explicit allowlist entry; everything else is read-only by default
- Every tool call is validated against a schema before it runs, so a hallucinated argument never reaches the database
- Destructive actions need a human confirmation step, no exceptions
The prompt still matters — it tells the agent what good work looks like — but the prompt is not the security layer. The code around the model is.
A small agent loop, in plain Python
Here is roughly what my loop looks like, stripped down to the shape of it:
MAX_STEPS = 10
def run_agent(user_request, tools):
history = [{"role": "user", "content": user_request}]
for step in range(MAX_STEPS):
reply = llm.chat(history)
if not reply.tool_calls:
return reply.content # The agent is done
for call in reply.tool_calls:
tool = tools.get(call.name)
if tool is None or call.name not in ALLOWED_TOOLS:
raise ValueError(f"Blocked tool call: {call.name}")
result = tool(**call.arguments)
history.append({"role": "tool", "name": call.name, "content": str(result)})
raise RuntimeError("Agent hit the step limit — stopping, not guessing")
Two things in that loop do most of the safety work: the allowlist check before any tool runs, and the step limit that stops the agent instead of letting it spiral. Everything else is observability.
Watch what it does, not just what it says
The agent's final answer is the least interesting part. What I actually review is the trace: which tools it called, in what order, with what arguments, and whether it stopped when it should have.
- Log every tool call with its arguments and result
- Review traces weekly — the failures teach you more than the successes
- Track how often the agent hits the step limit, calls a blocked tool, or asks for a tool it doesn't have
- Feed the real failures back into better tool descriptions and stricter rules
The pattern I keep coming back to is the same one as with any LLM feature: a small eval set of realistic tasks, a number that goes up or down, and a habit of rerunning it when I change anything.
What I would do first
If you are adding an agent to an existing app, do not start with a framework. Start with this:
- Pick one task your users already do by hand, with a clear end state
- Build two or three narrow tools for it — read-only first
- Put the loop behind an allowlist and a step limit, and log everything
- Run it on ten real examples and read every trace before you widen access
An agent is not magic. It is a model, a loop, and a set of tools — and the tools and the loop are the parts you actually own. Get those right, and the agent is useful. Skip them, and the agent is a demo that can delete your data with confidence.
Build the guardrails first, and the agent earns its freedom one tool at a time.
來源:DEV Community · dev.to