Illustration

An agent is pretty much a model in a loop. It reads its context, picks a tool, gets the result back, and repeats until it decides it’s done.

Sounds simple enough, but there’s quite a bit to think about when building one.

Let’s say you’re building a customer support agent. It can look up orders, answer questions, process refunds, etc.

A few things to think about here -

Control: which steps the model decides, and which code decides

Give the model as little autonomy as the task allows.

Start with plain code. Add a single LLM call where judgment is needed. Only use loops when the path really can’t be known in advance.

For example, if a customer asks for a refund, you don’t need an agent to figure out every step. Fetching the order, checking the refund policy, and validating eligibility can all happen in code. Let the model figure out what the customer is asking for and decide what to do next.

Every decision left to the model can vary from run to run. Code does the same thing every time and can be tested.

Good reliable agents are generally deterministic code with LLM steps sprinkled here and there.

Anything that must happen goes in code, not in the prompt

If a step is required on every run, run it in code before the model starts (prefetch).

For example, if every conversation needs the customer’s account details and permissions, fetch those upfront. Don’t rely on the model to remember to call a tool for something that’s required every single time.

That said, only prefetch what every run needs. No point loading things that most runs won’t use.

Don’t bloat the context

This one is kind of obvious but give the model the smallest set of information that lets it decide well. Load the rest only when it’s needed.

A few ways to think about this:

  • Facts every run needs → prefetch them (make sure the cost is justified here, else prefer loading on demand).
  • Facts only some runs need → a tool the model calls. For example, order history or shipping details.
  • A large tool server only some runs need → just show a one-line summary and load the full tool list only when it’s actually needed.

No need to give the model everything upfront.

Design tools around the agent’s tasks, not your API

A few tools, each doing one meaningful job end to end.

It’s tempting to just expose all your existing API endpoints as tools, but that doesn’t necessarily make a good toolset for an agent.

For example, instead of exposing separate tools for fetching an order, checking its status, calculating refund eligibility, and getting the refund amount, you could have a single get_order_refund_details tool that does all of that and returns what the agent needs.

Think about what the agent is actually trying to accomplish, not how your backend APIs happen to be structured.

Let errors guide the agent

Don’t bloat context in tool descriptions. But don’t neglect writing good tool descriptions either (what it does, when to use it, when to use something else) - this is vv important.

Let the responses correct the model instead of returning vague error codes.

For example, if it tries to refund an order outside the refund window, don’t just return 400 BAD REQUEST. Tell it why the refund isn’t allowed and what it can do instead.

When dealing with large responses, spill the output into a file and give the model shell access to perform file operations on it and infer from it. No need to dump everything into context just so it can find the one thing it needs.

Local tools vs. MCP tools

A local tool is one the agent’s worker runs itself, using the run’s own session.

For example: get_customer_details, generate_return_label.

An MCP tool is one the agent asks the MCP server to run. The server holds the keys and checks the rules.

For example: issue_refund (payment provider credentials stay on the server, and the server enforces refund limits and authorization).

Either type of tool could do the same work. Pick MCP when the tool needs a key the agent shouldn’t hold, a rule every caller must follow, or when other clients (Claude/Codex) need to use it too.

Otherwise, make it local.

Output and guards

Be explicit about what the agent must return and what it must never do.

For example, maybe every support conversation needs to return a structured response with the resolution, whether a refund was issued, and whether the issue needs human review.

Validate that output in code instead of hoping the model always follows the expected format.

Similarly, the agent should never be able to issue a refund above a certain amount or access another customer’s orders. Don’t rely on prompt instructions for these. Enforce them in code.

Measurement

How do you know it actually works?

Success rates, failure cases, tool selection, regressions, etc.

This is probably a whole topic on its own so I’ll leave it out of scope for now.

As a closing note, none of this is set in stone as the best way of doing things. But generally, these should help with better structure and consistency.

❗

I write to learn, not to teach. If I get something wrong, we get to learn together. Please reach out to me 🙂.