Why function calling beats prompt-and-parse
Without function calling: you ask the LLM to "extract the stock price," it returns "TCS is 4200 rupees," and you parse that text with regex. Fragile. The model might say "rupees 4200" or "4200" or "around 4200"—your regex breaks.
With function calling: the LLM sees your tools, picks one, fills in the arguments, and commits to a structured choice. You get a JSON response that matches the function signature. The model cannot hallucinate a tool that doesn't exist—Ollama's constraint layer blocks it.
Priya is building an agent for Indian retail traders. User asks: "What's TCS in dollars?" The agent must fetch TCS stock price (₹), convert it to USD, and return both values. Two tools: get_stock_price(ticker: str) and convert_currency(amount, from_, to).
Without function calling, Priya would have to coax the LLM to output text like: "amount=4200 currency=INR target_currency=USD". Then parse it. The LLM might write "4200 INR to USD" instead. Priya rewrites her parser. She spends a day on string matching.
With function calling: The LLM sees both tools, picks get_stock_price("HDFCBANK.NS") first, then convert_currency(4200, "INR", "USD"). The response is JSON. Priya's code: zero regex, zero guessing, 100% reliable.
Function calling is the difference between agents that work and agents that need babysitting. It's how Claude, GPT-4, and every production LLM route to functions. Learning it now with Ollama means your agents are production-ready from day one.
How it works: the two-step dance
Function calling is not magic—it's a structured conversation. Step 1: you send the user message + your tools to the LLM. The LLM reads your tool definitions and returns a choice: "run get_stock_price with ticker='HDFCBANK.NS'". Step 2: you run that function, then send the result back. The LLM sees the result and answers the user.
Step 1: Define your tools
Tools are functions you expose to the LLM. In Ollama's function-calling syntax, each tool is a JSON schema that describes its name, description, and parameters. Pydantic models make this easy: use model_json_schema() to export the schema Ollama needs.
Step 2: The LLM responds with tool_calls
Ollama returns a response with a tool_calls field—a list of tools to invoke, each with arguments. The LLM has committed to a choice. No ambiguity.
Step 3: You run the tool
Your code calls the function with those arguments. The result is a string or JSON—the tool's return value.
Step 4: The callback
Send the result back to the LLM as a new message. The LLM now has context (user message, tool choice, tool result) and can answer the user or pick another tool.
Your first tool: get_weather
Let's build a simple weather agent. Define a tool that takes a city name and returns a fake weather string. Walk through the whole flow: define the tool, ask the LLM a question, handle the response.
Multi-tool example: picking the right tool
Now add a second tool. Ask the LLM: "What's TCS in dollars?" The LLM must pick get_stock_price first, then convert_currency. The agent routes correctly because the LLM reads the tool descriptions and picks the one that fits.
Common bug: forgetting the tool result callback
This is the most common mistake: you run the tool, but forget to send the result back to the LLM. The LLM never sees it, so it hallucinates an answer. Or it loops forever, picking the same tool again.
{"role": "tool", "content": result, "tool_name": "..."} before the next LLM call. Forget this, and your agent breaks.
Pydantic to JSON schema: one line of code
Pydantic models are the easiest way to define tools. Use model_json_schema() to get the exact JSON schema Ollama needs. No manual JSON—let Python do it.
Error handling: LLM hallucinates, you catch it
Sometimes the LLM picks a tool that doesn't exist, or returns arguments that don't match the schema. The agent must handle this gracefully: log the error, retry, or fall back to a simpler approach.
Why agents need function calling
An agent is a loop: observe state → pick an action → execute → observe new state → repeat. Without function calling, the LLM can't reliably pick actions. It guesses, you parse, things break.
With function calling, the LLM commits to a choice. Your agent is:
- Predictable. The LLM picks from your tools—not random text.
- Typed. Arguments match the function signature.
- Composable. One agent calls many tools, chaining results.
- Debuggable. Every tool call is logged as JSON. You see exactly what happened.
How this connects to real AI agents
Every production LLM (Claude, GPT-4, Gemini) uses this exact pattern. By mastering function calling with Ollama now, you're learning the universal agent pattern. In Lesson 08, you'll add real tools: stock APIs, web search, file I/O. In Lesson 09, multi-agent teams. Function calling is the foundation of it all.
Task: Define a Pydantic model for ConvertCurrency(amount: float, from_: str, to: str). Export its schema with model_json_schema(). Call your LLM with this tool and ask: "Convert 1000 KES to INR". Capture the tool_calls from the response.
Hint 1: Define the model using BaseModel with three Field() fields.
Hint 2: Use model_json_schema() to export.
Hint 3: Pass {"name": "convert_currency", "schema": schema} to the tools parameter.
Hint 4: Print response["tool_calls"] to see what the LLM picked.
Test your knowledge
You've learned tool picking.
Now build agents that scale.
Join the Best AI Program: The AI-Era Agentic Engineer — 5 months, fully online, with placement assistance. Week 1 is free.
Explore The AI-Era Agentic Engineer →