HOW-TO · SUP

How to set up agent tool use with function calling

intermediate20 minBy Fredoline Eruo
Target environment
Ubuntu 24.04 · Ollama 0.4.x
PREREQUISITES

Ollama with function calling support, tool definitions

What this does

Setting up agent tool use with function calling enables an AI agent to invoke external tools—such as calculators, databases, file systems, and APIs—through a structured function-calling interface. The model receives tool definitions as JSON schemas, and when it determines a tool is needed, it outputs a structured function call rather than free-form text. The agent runtime intercepts this call, executes the tool, and feeds the result back to the model. This enables deterministic, parseable tool interactions compared to parsing tool invocations from plain text.

Steps

Define tool schemas. Each tool requires a JSON schema with the tool name, a natural language description, and a parameters object with properties and required fields. Example calculator tool: {"type": "function", "function": {"name": "calculator", "description": "Evaluate a mathematical expression", "parameters": {"type": "object", "properties": {"expression": {"type": "string", "description": "The math expression to evaluate, e.g. '2+2'"}}, "required": ["expression"]}}}. Implement the tool executor: a dictionary mapping tool names to handler functions. def calc_handler(expression): return str(eval(expression)) with safety by restricting eval to a whitelisted set of operations using eval(expression, {"__builtins__": {}}, {"abs": abs, "round": round}). Send a request with tools to Ollama: response = ollama.chat(model="llama3.1", messages=messages, tools=[calculator_tool, weather_tool, file_read_tool]). After receiving the response, check if response["message"].get("tool_calls"). Each tool call has function.name and function.arguments. Execute and create a tool response message: tool_message = {"role": "tool", "content": result, "name": tool_name}. Append both the assistant message (containing the tool call) and the tool message to the conversation history. Call the model again so it can synthesize the tool result into a final answer. Handle edge cases: if the model returns a final answer instead of a tool call, end the loop. If no tool calls are present in the response, treat the message as the final answer. Implement a loop limit of 10 tool-call cycles to prevent infinite loops. Add error handling: if tool execution fails, return the error string as the tool result so the model can retry or report the failure.

  • Record the local run evidence. Save the exact command, runtime or package version, model name if applicable, and observed output so the result can be reproduced later.

  • Confirm the local starting state. Print the active binary, package version, model name, or configuration path before changing the workflow.

  • Run the smallest complete path. Execute the minimum command or script that proves the guide works end to end on the local machine.

  • Compare against expected output. Check the final line, status code, generated artifact, or model response against the verification section before expanding the setup.

  • Record the local run evidence. Save the exact command, runtime or package version, model name if applicable, and observed output so the result can be reproduced later.

Verification

Send a query requiring a tool: "What is 15 * 7 + 3?" Verify the model returns a tool call for the calculator with expression "15 * 7 + 3". The tool executor should return "108" and the model should synthesize this into a final answer. Test a query that does NOT require tools: "Hello" should return a direct response with no tool calls. Test tool error handling: create a tool that always raises an exception and verify the model gracefully handles the error message. Run 5 different tool-requiring queries and verify each returns correct tool results and final answers.

Common failures

Model does not emit tool calls: Verify the model supports function calling—check the model card; for Ollama, use ollama show <model> and confirm "tools" support. Tool schema mismatch: The required field must match parameter names exactly; optional parameters should not be in the required array. Tool call arguments are not valid JSON: Some models output malformed JSON—add a retry with format="json" in the chat options or implement a JSON fixer that adds missing braces. Tool execution crashes the agent: Always wrap tool execution in try/except and return error messages as tool response content so the loop continues. Context window fills up: Limit conversation history to last 20 messages or summarize earlier context before appending tool results.

  • Version mismatch - The installed package or runtime differs from the command shown; check the version first and rerun the smallest verification command.
  • Local environment drift - Another service, virtual environment, model, or path is being used; print the active binary path and configuration before changing the guide steps.

Related guides

  • build-langgraph-agent-scratch
  • implement-web-search-ai-agents
  • build-multi-agent-supervisor-workflow