AI Engineering
Building reliable AI agents: tools, limits and a way out
How to design AI agents that behave in production: small typed toolsets, step and cost budgets, validated arguments, escalation and trajectory evaluation.
By Envexa Engineering7 min read
An AI agent is a loop: a model looks at a goal and the current state, chooses a tool to call, receives the result and repeats until it decides it is done. The power of that loop is also its risk. Each iteration is a chance to choose the wrong tool, pass the wrong arguments, or never stop. Reliable agents are mostly the result of engineering around the model.
Start with a small, typed toolset
Every tool should have a narrow purpose, a precise description and a typed schema for its arguments. Five well-described tools are chosen correctly more often than fifty overlapping ones. Prefer tools that are read-only by default; separate read tools from write tools so permissions can differ.
class LookupOrder(BaseModel):
order_id: str = Field(pattern=r"^ORD-\d{6}$")
@tool(scope="orders:read")
async def lookup_order(args: LookupOrder, ctx: Context) -> OrderSummary:
order = await orders.get(args.order_id, customer_id=ctx.customer_id)
if order is None:
raise ToolError("order_not_found")
return OrderSummary.from_order(order)Note that customer_id comes from the authenticated context, not from the model. The model can propose arguments; your code decides what it is allowed to access.
Put budgets on everything
- Step limit: a maximum number of loop iterations per task.
- Tool-call limit: per tool and per conversation, which catches repeated calls.
- Token and cost ceiling: stop and escalate when exceeded.
- Time limit: end-to-end deadline, with a graceful message.
When a budget is hit, the agent should not improvise. It should stop, summarise what it did and hand over.
Treat all text as untrusted
User messages, retrieved documents and tool outputs can all contain instructions that try to redirect the agent (prompt injection). Do not rely on the model to resist. Validate tool arguments server-side, enforce permissions in code, require explicit confirmation for sensitive or irreversible actions, and avoid giving a single agent both access to private data and the ability to send data outward.
Design the way out
Escalation to a person is a feature, not a failure. Define it as rules: low retrieval confidence, repeated tool errors, sensitive categories (payments, legal, safety), an explicit request, or an exhausted budget. The handover should include the transcript, the actions taken and the retrieved context, so the customer does not start again.
Log what happened, not what the model says about itself
Record operational events: tool called, arguments, result status, duration, tokens. That trace is auditable and useful for debugging. Free-text model explanations of its own reasoning are neither reliable nor needed for operations, so do not build on them.
Evaluate trajectories
A single reply can look fine while the path that led to it was wrong: an unnecessary tool call, a skipped check, an unsafe action. Write scenarios with expected tool sequences and outcomes, such as an order status lookup, a refund outside policy, a prompt-injection attempt, and run them in CI. Grade the trajectory as well as the final answer.
Summary
A reliable agent has few, typed tools; permissions enforced in code; budgets for steps, cost and time; deterministic escalation rules; operational tracing; and scenario-level evaluation. The model supplies flexibility. Everything else supplies safety.
Related articles
RAG architecture: the decisions that matter before you pick a vector database
Retrieval-augmented generation succeeds or fails on chunking, retrieval quality and evaluation, not on your vector database choice. A practical guide.