Function calling
Function calling lets a chat model request your code — weather lookups, order databases, refunds — then turn the results into a natural reply.
In this guide
- Extend Build an agent with robust patterns
- Run independent tools in parallel for lower latency
- Control behavior with
tool_choiceset toauto,any, ornone - Handle tool errors with re-prompts that keep the chat going
Define two Kathmandu tools
Declare each function as a JSON schema with a name, description, and parameters. This example pairs a Kathmandu weather lookup with an order-status query — one public, one private.
Use sage-2-4-actus when calls depend on Nepali nuance or multi-step reasoning. Full tool schemas live in the Chat Completions reference.
Run independent calls in parallel
When the model returns two tool_calls in one turn — weather plus order — execute them concurrently, not sequentially. Append each result as a tool message with the matching tool_call_id, then send everything back for the final answer.
Parallel calls cut p95 latency roughly in half for two-tool turns. Only chain sequentially when the second call needs the first call output.
Steer with tool_choice
tool_choice decides whether the model may, must, or must not call functions.
| Value | Behavior | When to use |
|---|---|---|
auto | Model decides per turn | Default support bots with optional lookups |
any | Must call at least one tool | Grounded answers — no reply without data |
none | Plain chat, tools ignored | Creative writing, greetings, FAQs |
Start with auto. Switch to any for flows like refunds or KYC where an ungrounded answer is worse than a slower one. Use none for a fast classification pre-pass on sage-2-5-celer before escalating to a tool-enabled sage-2-4-actus turn.
Recover from tool errors
Tools fail — expired order IDs, timeouts, bad districts. Return the error as tool content, not as an exception, and let the model re-prompt the user gracefully in Nepali.
Cap retries at two per tool per turn. On repeated RETRYABLE errors, answer with cached data or escalate to a human instead of looping. For the full starter flow, revisit Build an agent.
Best practices
- Give every tool a one-line Nepali-aware description with an example input.
- Validate arguments in code before hitting your database.
- Keep tool results small — summarize rows, never dump full tables.
- Log every
tool_call_id, latency, and error code for debugging. - Test
autovsanyon 50 real utterances before locking a default.
