Xavier Fok
← All writing

· 7 min read

Tool design for agents: how to stop a model from misusing your functions

A practical look at why AI agents call the wrong tool with the wrong arguments, and how to design function signatures, names, and errors so they stop.

The bug is usually in the function, not the model

When an agent calls a tool wrong, the instinct is to blame the model. Swap it for a bigger one, add a stricter system prompt, throw in a few-shot example. Sometimes that helps. More often the real problem is sitting in the function signature you handed the model in the first place.

I run agents against real tools every day: MCP servers that touch render queues, publishing pipelines, customer infrastructure, file systems. When something misfires, the postmortem almost always lands on the same thing. The function was ambiguous, the parameters didn’t say what they meant, or the error message told the model nothing it could use. Fix the function, the misuse stops. Not always, but often enough that it’s the first place I look now, before I touch the prompt.

This matters because of how tool calling actually works under the hood. The model doesn’t see your code. It sees a name, a description, and a JSON schema for the arguments. That’s the entire interface. If that interface is vague, the model fills the gap with a guess, and the guess is shaped by whatever pattern looks statistically closest to what it’s seen before, not by what your function actually does.

What misuse actually looks like

It rarely looks like the model doing something bizarre. It looks like the model doing something reasonable, for the wrong function.

A few patterns I’ve hit repeatedly:

  • Wrong function picked from a crowded toolbox. Two tools that both sound like they “update” something, and the model reaches for the one it saw first or the one with the shorter description.
  • Right function, wrong arguments. A parameter called id when there are three different kinds of IDs in the system, and the model passes the one that happened to be in context.
  • Right function, missing a required precondition. The model calls delete_port without first checking port_status, because nothing in the tool description told it that order mattered.
  • Silent partial success treated as failure, or vice versa. The function returns an empty list on both “nothing found” and “you don’t have permission,” and the model can’t tell which one happened, so it retries the wrong thing or gives up too early.

None of these are the model being careless. They’re the model doing exactly what an underspecified interface invites it to do.

Narrow the surface area before you narrow the prompt

The first fix, and the one people reach for last, is to just have fewer functions doing more precise things instead of a handful of “flexible” ones. A single manage_resource(action, target, params) function that branches internally into eight behaviors is convenient to write, but it pushes all the disambiguation work onto the model at call time. It has to guess the right action string, the right shape of params for that action, and it has no schema to lean on because the schema is the same generic blob for every branch.

Split it. create_x, delete_x, list_x, update_x_status. Each one gets its own name, its own description, its own tightly typed arguments. This feels like more surface area but it’s actually less ambiguity per call. The model isn’t reasoning about which action string to pass inside a wrapper, it’s picking from a menu of specific, named things, and JSON schema can enforce the shape of each one separately.

I noticed this directly with a render pipeline tool that used to take a generic command string. Once it got split into distinct calls, the wrong-tool-picked failures mostly went away, not because the model got smarter but because there was less room to be wrong.

Names and descriptions carry more weight than the code does

The model never reads your implementation. It reads the name and the description string, and it makes a judgment call in a fraction of a second about which tool matches the user’s intent. Treat that string like you’re briefing someone who has three seconds and no context.

Vague: “Updates a resource.” Better: “Marks a port as reserved so provisioning skips it. Does not delete or reassign the port.”

The second one tells the model what the function does, and just as important, what it doesn’t do. A lot of misuse comes from the model assuming a function has a side effect it doesn’t have, or lacks one it does have. Say the negative case out loud in the description if it’s the kind of thing someone would reasonably assume.

Same goes for parameter names. id is almost never a good parameter name if there’s more than one kind of identifier anywhere near this tool. port_id versus customer_id versus imei costs you four extra characters and saves the model from mixing them up. I’ve watched an agent pass a customer-facing order reference into a field that expected an internal database ID, because both were just called ref in two different tools it had access to in the same session.

Make the wrong call impossible to construct

JSON schema gives you more leverage than most people use. Enums instead of free strings wherever the set of valid values is fixed. Required fields marked required, not just mentioned in a docstring. Nested objects instead of a flat bag of optional parameters where three of them are only valid in combination with each other.

If a function only makes sense when called with status="active" or status="paused" and nothing else, an enum with those two values stops the model from ever passing status="running" because it sounded plausible. That’s not a prompt engineering win, it’s a type system doing its job. The fewer valid shapes the argument can take, the fewer ways there are to misuse the call, independent of how good the model is that day.

Where the schema genuinely can’t express a constraint, like “this field is required only if that other field is set to X,” say it plainly in the description rather than leaving it implicit. Models are decent at following an explicit conditional rule in a description. They’re bad at inferring an implicit one from field names alone.

Errors are part of the interface, not an afterthought

This is the one people skip. When a tool call fails, whatever string comes back is the only signal the model gets about what went wrong and what to try next. “Error: invalid request” tells it nothing. It’ll either retry the identical call, which fails again the same way, or it’ll guess at a fix that has nothing to do with the actual problem.

Compare that to something like “Error: port_id 4021 is currently assigned to customer 118. Unassign it first with unassign_port before deleting.” That’s not just more polite, it’s functionally a second chance for the model to recover without you in the loop. It names the actual constraint that got violated and it names the specific tool that resolves it. I’ve had agents self-correct within the same turn off an error message like that, with zero extra prompting, simply because the error message did the work a person would otherwise have to do by jumping in and explaining.

Treat your error strings as prompts you’re writing for the model, because that’s what they are the moment an agent is calling this function instead of a human reading a stack trace.

Test it like you’d test a new hire’s understanding, not like you’d test code

Unit tests check that your function does the right thing when called correctly. They don’t check whether an agent will call it correctly in the first place. The only way to find that out is to actually run agent sessions against the tool and watch what it tries, including the wrong attempts, before you ship it to something that matters.

The failures worth paying attention to aren’t the ones where the model clearly hallucinated a function that doesn’t exist. Those are rare and easy to spot. The ones worth fixing are the near misses: right function, slightly wrong argument, or two functions the model treated as interchangeable when they weren’t. Those are exactly the failures that a sharper name, a stricter schema, or a more specific error string will quietly fix, without you touching the model or the prompt at all.

None of this makes an agent infallible, and it shouldn’t be sold that way. A well designed tool reduces the ways a model can go wrong. It doesn’t eliminate judgment calls, and it doesn’t replace someone checking the output before it touches anything that can’t be undone. What it does is make the model’s job smaller and clearer, which is the same thing that makes any interface easier to use correctly, whether the caller is a person or a model.

If you want to see how these principles play out on actual render pipelines, MCP setups, and agent tooling I run day to day, there’s more on the channel and the rest of the site here.

Get new guides and videos first — join the Telegram channel.

Keep reading