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.

📈
Real-world scenario
Priya (Bangalore): building a stock price agent

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.

User msg + tools
→
LLM picks tool
→
You run it
→
Send result back
→
LLM answers

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.

Key insight:
The LLM never directly modifies your code. It only suggests tool calls. Your code controls execution. This is safe by design.

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.

weather_tool.py Define and use your first tool
Expected flow
Step 1 — LLM picks tool: {"tool_calls": [{"name": "get_weather", "arguments": {"city": "Hyderabad"}}]} Step 2 — Tool runs: Tool result: Sunny, 32°C Step 3 — LLM answers: Final answer: The weather in Hyderabad is sunny and warm at 32°C.
Notice:
The LLM never returned weather data directly. It picked a tool, you ran it, the LLM saw the result, and only then answered the user. That's the power of function calling.

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.

multi_tool_agent.py Two tools, one agent flow
Expected output
LLM picks: [{"name": "get_stock_price", "arguments": {"ticker": "TCS.NS"}}, ...] Tool 'get_stock_price' result: TCS.NS is ₹3850 Tool 'convert_currency' result: ₹3850 INR = $46.67 USD Agent answer: TCS is trading at ₹3850 (approximately $46.67 USD).

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.

bug_demo.py What happens when you forget the callback
Rule:
Every tool result must be appended to the messages list as {"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.

schema_export.py Export Pydantic model to Ollama schema
Why this matters:
No manual JSON = no typos in field names = the LLM always knows what arguments to pass. Pydantic validates types. This is how production agents work.

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.

error_handling.py Catch hallucinations and retry

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.

Multi-Agent Systems Tool Routing Reliability Production Agents
Mini Challenge: Add a currency tool

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

Question 1 of 5
What is the main benefit of function calling over prompt-and-parse?
Question 2 of 5
In the two-step dance, what comes after the LLM picks a tool?
Question 3 of 5
What does Pydantic's model_json_schema() return?
Question 4 of 5
What is the most common bug in function calling agents?
Question 5 of 5
If the LLM picks a tool that doesn't exist, how should your agent respond?
0
/5
Excellent! You understand function calling and agent foundations.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

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 →