Tools in WhatsApp agents: function calling

How to give a WhatsApp agent tools: schema definitions, the tool_use and tool_result loop, errors, a turn cap and safer tools.

An agent that only talks answers. An agent with tools looks up an order, opens a ticket or calls a person. The difference is tool use, also called function calling, and it is where a WhatsApp agent stops being a talking FAQ and starts doing things in your system. It is also where mistakes get expensive.

What is a tool and who runs the code?

A tool is a function you describe to the model, with a name, a description and an input schema. The model never executes anything: it returns a structured request saying which tool it wants to call and with which arguments, and your code runs it and returns the result.

According to Anthropic’s documentation, tools split into two families by where they run. Client tools, which include the ones you define, run in your application: the model replies with stop_reason equal to tool_use and one or more tool_use blocks, your code executes, and you send a tool_result. Server tools, such as web search, run on Anthropic’s infrastructure. For a WhatsApp agent that queries your systems, you work with client tools.

How do you define a tool the model uses well?

With a description that says what it does, when to use it and when not to, and a strict input_schema. The description guides the model’s decision; a nice name with a vague description yields wrong or missing calls.

const tools = [ { name: 'consultar_pedido', description: 'Looks up the status of ONE order by number. Use when the customer gives ' + 'or asks about an order. It cannot change anything.', strict: true, input_schema: { type: 'object', properties: { numero: { type: 'string', description: 'Order number, digits only' } }, required: ['numero'], additionalProperties: false, }, }, { name: 'transferir_para_humano', description: 'Hands the conversation to a team member. Use if the customer asks, if you ' + 'cannot answer or if the topic is a complaint, cancellation or payment.', strict: true, input_schema: { type: 'object', properties: { motivo: { type: 'string', description: 'One sentence with the reason' } }, required: ['motivo'], additionalProperties: false, }, }, ]

Setting strict to true guarantees, according to the documentation, that calls match the schema exactly, which removes missing parameters and wrong types. If the model is going to call a tool that writes somewhere, that kind of guarantee is worth the care of declaring the schema.

  • Few tools, well separated. Each definition becomes input tokens on every request.
  • One tool, one responsibility. consultar_pedido should not also cancel.
  • Describe arguments with format examples, such as digits only.
  • Prefer read tools. Grant write access only where the gain justifies the risk.

On cost: the documentation states tool use adds tokens, coming from the tool definitions, the tool_use and tool_result blocks, and a special system prompt that tool use itself includes. For Claude Sonnet 5.5, that fixed addition is 286 tokens with tool_choice set to auto. It is small, but it adds to your definitions on every reply.

How does the tool-call loop work?

It is a loop of asking the model, running what it asked for and returning the result, repeated until the model answers with text. The end is marked by a stop_reason other than tool_use. Every production loop needs a turn cap.

async function responderComFerramentas(claude, sistema, mensagens, executar) { for (let volta = 0; volta < 5; volta++) { // turn cap: never an infinite loop const r = await claude.messages.create({ model: 'claude-sonnet-5-5', max_tokens: 500, system: sistema, tools, messages: mensagens, }) if (r.stop_reason !== 'tool_use') { return r.content.filter((b) => b.type === 'text').map((b) => b.text).join('\n').trim() } mensagens.push({ role: 'assistant', content: r.content }) const resultados = [] for (const bloco of r.content.filter((b) => b.type === 'tool_use')) { try { const saida = await executar(bloco.name, bloco.input) // YOUR code runs here resultados.push({ type: 'tool_result', tool_use_id: bloco.id, content: JSON.stringify(saida) }) } catch (e) { resultados.push({ type: 'tool_result', tool_use_id: bloco.id, is_error: true, content: 'Failed: ' + e.message + '. Tell the customer the lookup is unavailable right now.' }) } } mensagens.push({ role: 'user', content: resultados }) // tool_result comes first } return null // cap reached: call a person }

The documentation highlights three format rules that cause errors when broken. The tool_result must come immediately after the assistant message that requested the tool, with no messages in between. In the user message carrying the results, tool_result blocks come first and any text after. And each tool_result references the id of the matching tool_use.

Error handling deserves attention. When the tool fails, return a tool_result with is_error set to true and an instructive message saying what went wrong and what the model may try or say. The documentation recommends this over generic messages such as “failed”. In the example, the message already tells the model to inform the customer the lookup was unavailable.

How do you control when and how many tools the model calls?

With the tool_choice parameter. The default auto lets the model decide each turn; any requires using some tool; tool requires a specific one; and disable_parallel_tool_use set to true limits it to at most one call per turn. For most support agents, auto with parallelism off is the most predictable starting point.

NeedSettingNote
Model decidestool_choice auto (default)May answer directly without a tool
One call at a timedisable_parallel_tool_use: trueEasier to debug and to limit side effects
Force one tooltool_choice of type toolFor fixed steps, such as classifying
More tool useSystem prompt instructionThe documentation cites light and strong phrasings

The documentation also notes that, if a required parameter is missing, some models may infer a value instead of asking. In a WhatsApp agent, that means a missing order number can turn into an invented one. Instruct in the prompt: if data is missing, ask the customer.

How do you keep tools safe?

By applying least privilege, validating in your code rather than in the prompt, and treating tool output as untrusted data. The model can be misled by customer text or by content a tool returns; your real barrier is the code that executes.

  • Authorization in the executor: the tool looks up the order of the contact in the conversation, identified by the channel and the event sender, never by an id the model supplies freely.
  • Idempotency on writes: the model may repeat a call, and the job may run twice. Use idempotency keys in your internal actions too.
  • Confirmation before the irreversible: for actions that cannot be undone, have the tool return a confirmation request and ask the customer for an explicit yes, for example with buttons.
  • Output as data: the documentation advises delivering external content in tool_result and stating in the system prompt that it cannot change the instructions.

The security article in this series goes deeper on prompt injection. For the mechanics of sending the final reply, see the quickstart, and for credentials the authentication page. The handoff tool replaces the text marker used in the Hub guide example.

Frequently asked questions

Does the model run the tool?

Not for tools you define. It returns a tool_use block with a name and arguments, and your code executes and replies with a tool_result.

How many tools can I give the agent?

The documentation consulted sets no limit for this purpose. In practice each definition consumes tokens on every request, so keep them few and well described.

What do I do when the tool fails?

Return a tool_result with is_error true and a message saying what went wrong and what the model should do, such as telling the customer. On repeated failures, hand off to a person.

How do I avoid an infinite loop of calls?

With a turn cap in your code. If it is exceeded, stop, log and hand the conversation to a person.

Fontes

Documentação oficial da Meta, conferida em 31 de outubro de 2026.