Hard-won rules
- Validate every tool call. Models hallucinate function names, miss required args, supply wrong types. Wrap dispatch in try/except and return error JSON to the model rather than crashing.
- Keep the tool set small. 5–10 tools is a sweet spot. Beyond that, the model gets distracted and picks wrong. If you need more, group tools by domain and pick a subset per request.
- Write descriptions like docstrings. First sentence: what it does. Second sentence: when to use it (and when not to).
- Test with
stream: falsefirst. Streaming + tool calls adds parsing complexity. Get the loop right non-streaming, then layer streaming on top. - Use a registry. A
{name: callable}dict beats a giant if/else chain — easier to test, easier to extend, easier to replace one tool.
The Ollama Python library shortcut
Since v0.4 the official library accepts Python functions directly as tools. It auto-generates the JSON Schema from the function signature and docstring. For prototyping, this is a huge accelerator. For production, define the schema explicitly so descriptions are intentional.