Building Your First AI Agent with Function Calling
Updated: 3 days ago

UIT University 365 Institute of Technology
Series AI Agents Series
Duration 25 minutes
IT Engineering, AI and Applied AI, Data Science, Software Development, Digital Transformation

UNOP Sound (University 365 Neuroscience Oriented Pedagogy)
Take five minutes to prepare your brain. Play the isochronous tone track (40Hz gamma frequency) with your eyes closed. Gamma-frequency tones before a learning session raise attention and make the material easier to absorb.
[Audio player: UNOP Pre-Lecture Isochrone (40Hz, 5 minutes)]
In this Lecture
The Hook: From Chatbot to Agent
A chatbot can talk. An agent can act. The difference is one mechanism: function calling.
When you ask a chatbot "what is the weather in Paris?", it generates text based on its training data. It might be right or wrong. It cannot check. When you ask an agent the same question, it calls a weather API, gets the real current temperature, and tells you the answer. The agent does not guess. It knows.
Function calling is the bridge between language models and the real world. It lets the model ask your code to run a function: query a database, call an API, read a file, send an email. The model decides which function to call and with what arguments. Your code executes the function and returns the result. The model uses that result to answer the user.
Every major agent framework (LangChain, CrewAI, AutoGen, OpenAI Assistants) is built on this primitive. Understanding function calling means understanding how all agents work.
In the next 20 minutes, you will build a working AI agent from scratch, understand the loop that powers it, and learn the production patterns that make agents reliable.

What Is Function Calling?
Function calling (also called tool use) is the mechanism by which a language model asks your application to run a piece of code on its behalf. You describe available functions to the model as JSON schemas. When the user request implies a function should run, the model returns a structured payload with the function name and a JSON object of arguments. Your code executes the function, appends the result to the conversation, and the model produces a final answer.
Key Misconception
The model does not execute your functions. It cannot run code, access your database, or make API calls. It only tells you which function to call and what arguments to use. Your code does the actual execution. The model is the decision maker. Your code is the actor.
The Four-Step Round Trip
1. You send the user message plus a list of tool schemas to the model.
2. The model responds with either a normal text answer or one or more tool calls (a tool_calls array).
3. You execute each requested tool in your own code and append the result as a message with role "tool".
4. You send the conversation back to the model, which now has the tool output and produces a final answer.
This round trip is the atomic unit of every AI agent. Everything else is orchestration around this loop.

The Agent Loop: Think, Act, Respond
The agent loop is the core of every agent. It is a cycle of thinking (model decides), acting (code executes), and responding (model answers or requests another tool call).
How the Loop Works
The loop continues until the model returns a plain text response with no tool calls. At that point, the agent is done and the text is the final answer for the user.
Why a Loop, Not a Single Call
Some questions require multiple tool calls. "Compare the weather in Paris and London" requires two weather API calls. "Find the cheapest flight to Tokyo, then book it" requires a search call followed by a booking call. The loop handles these multi-step workflows naturally: the model calls one tool, sees the result, decides what to do next, and continues until the task is complete.
Maximum Turns
Every agent loop needs a maximum turn limit (typically 5 to 10). Without it, a confused model can loop forever, calling the same tool repeatedly. The limit is a safety net, not a feature.

Defining Tools: JSON Schema Basics
Tools are defined as JSON objects with a name, description, and parameter schema. The model uses the description to decide when to use the tool, and the parameter schema to generate correct arguments.
Tool Definition Structure
A tool definition has three parts:
- type: Always "function" for function calling.
- function: An object with name, description, and parameters.
- parameters: A JSON Schema object describing the expected arguments.
Writing Good Descriptions
The description is the most important part. The model relies on it to decide when to call the function. Write descriptions that explain what the function does, when to use it, and what the parameters mean. Bad descriptions produce wrong tool calls.
Parameter Schema Tips
- Mark required parameters with "required": ["param_name"].
- Add descriptions to each parameter so the model knows what values to provide.
- Use "enum" for parameters that accept a fixed set of values (for example, units: "metric" or "imperial").
- Set "type" for each property (string, number, boolean, array, object).

Building Your First Agent: A Weather Assistant
Let us build a working agent that can check the weather for any city. This example uses the OpenAI Python SDK, but the pattern works with any provider that supports function calling (Anthropic Claude, Google Gemini, OpenRouter).
Step 1: Define the Tool
Define a weather function and its JSON schema. The function takes a city name and returns a weather string. In a real application, this function would call a weather API like OpenWeatherMap.
Step 2: Create the Tool Map
Map tool names to their Python implementations. This lets your code look up and execute the right function when the model requests it.
Step 3: Build the Agent Loop
The loop sends the user message to the model, checks for tool calls, executes them, and sends results back. It repeats until the model returns a final text answer or the maximum turn limit is reached.
Step 4: Test the Agent
Ask the agent: "What is the weather in Paris?" The model will call the get_weather function with city "Paris", your code executes it, returns the result, and the model gives you a natural language answer like "The current weather in Paris is 18 degrees Celsius with partly cloudy skies."
This is a complete agent. It has tools, a loop, and the ability to act on the real world through your code.

Handling Multiple Tool Calls
Modern models can request multiple tool calls in a single response. For example, if the user asks "Compare the weather in Paris, London, and Tokyo", the model may return three tool calls in one turn.
Parallel Execution
When the model returns multiple tool calls, execute them in parallel (using concurrent.futures or asyncio) and append all results before sending the conversation back. This reduces latency significantly for independent operations.
Sequential Tool Calls
Some tool calls are sequential: the result of one determines the arguments of the next. "Find flights to Tokyo, then book the cheapest one" requires two sequential calls. The loop handles this naturally: the model makes the first call, sees the result, then makes the second call in the next turn.
Key Rule
Always iterate over the tool_calls array. Never assume there is only one. Missing tool calls produces broken agents that skip steps.

Error Handling and Validation
Agents fail in production when they do not handle errors. Here are the three rules that prevent most failures.
Rule 1: Validate All Arguments
Never trust model-generated arguments blindly. The model can produce invalid JSON, wrong types, or out-of-range values. Validate every argument before executing the function. Use Pydantic or manual type checks. Reject invalid inputs and return an error message to the model so it can retry.
Rule 2: Return Errors as Strings, Not Exceptions
If a tool fails (API timeout, database error, invalid input), return the error as a string in the tool result, not as a Python exception. The model can read the error message, apologize to the user, try a different approach, or retry. If you raise an exception, the agent crashes.
Rule 3: Set Timeouts on Tool Execution
External API calls can hang. Set a timeout on every tool execution (typically 10 to 30 seconds). If a tool times out, return a timeout error string to the model. This prevents the agent from hanging indefinitely on a slow or unresponsive service.

Production Patterns: What Makes Agents Reliable
Building a toy agent is easy. Running an agent in production at scale is hard. These patterns separate prototypes from production systems.
Structured Output
Use JSON mode or structured output formatting when the agent needs to return data that downstream code will parse. This eliminates parsing failures and ensures the output matches your expected schema.
Logging and Tracing
Log every tool call, its arguments, its result, and the model's reasoning. In production, you need to debug why an agent made a specific decision. Without traces, you are guessing. Use OpenTelemetry, LangSmith, or a simple logging framework.
Rate Limiting
The model can call tools faster than your APIs can handle. Add rate limiting to tool execution to prevent overwhelming external services. This is especially important for agents that call paid APIs.
Human in the Loop
For tools with side effects (sending emails, making payments, deleting records), require human approval before execution. The agent proposes the action. A human approves it. Only then does the code execute. This prevents costly mistakes from model hallucinations.
Fallback Behavior
When a tool fails or the model cannot complete the task, the agent should degrade gracefully. Return a helpful message explaining what went wrong and what the user can do. Never crash silently.

Beyond Single Agents: Multi-Step Workflows
A single agent with tools is powerful. But some tasks require multiple agents working together, each with different tools and responsibilities.
Agent Roles
In a multi-agent system, each agent has a role. A research agent gathers information. A planning agent creates a step-by-step plan. An execution agent carries out the plan. A review agent checks the results. This separation of concerns produces better results than a single agent trying to do everything.
Handoffs
Agents hand off tasks to each other through structured messages. The research agent passes its findings to the planning agent. The planning agent passes its plan to the execution agent. Each handoff is a function call that invokes the next agent.
When to Use Multi-Agent
Multi-agent systems add complexity. Use them only when a single agent cannot handle the task within its turn limit, or when different parts of the task require different tools or expertise. For most tasks, a single well-designed agent is sufficient.

The Tool Contract: Who Owns What
The single mental model that prevents most agent defects is a contract between two parties that never negotiate at runtime.
The model decides what to call and with what arguments. Your code decides whether to execute it and what happens next. The model never runs anything. Your code never guesses what the model meant.
Stated as two obligations:
The model owes you a well-formed request. A tool name from the list you provided, arguments that match the schema you published.
You owe the model a usable result. A string it can read, a clear success or failure signal, and enough information to decide the next step.
Everything in this lecture follows from that split. When an agent misbehaves, the first diagnostic question is which side of the contract broke.
The Agent Loop, Written Out
The loop is small enough to write in full. Reading it once removes a lot of confusion about how agents behave.
messages = [system_prompt, user_message] for turn in range(MAX_TURNS): response = model(messages, tools=tool_schemas) messages.append(response) if not response.tool_calls: return response.text # the model decided it is done for call in response.tool_calls: name = call.name args = call.arguments if name not in TOOLS: result = f"Error: no tool named {name}. Available: {list(TOOLS)}" elif not valid_arguments(name, args): result = f"Error: invalid arguments for {name}. {describe(args)}}" else: result = run_with_timeout(TOOLS[name], args) messages.append(tool_result(call.id, result)) raise BudgetExceeded(f"no final answer within {MAX_TURNS} turns")
Five properties of this loop are worth naming, because each one corresponds to a defect it prevents.
The tool list is the source of truth. The model can only choose from TOOLS. A call to anything else is a refusal with the valid names attached, which lets the model correct itself in one turn.
Validation happens before execution, not after. A malformed call never reaches your real system.
Every path returns a string. The error branches and the success branch produce the same type. The loop has no exception path through it.
The conversation is the state. There is no separate agent memory in this loop: the message list is the whole state, which makes it trivially loggable and replayable.
The turn budget is enforced by the loop, not requested of the model. MAX_TURNS is a number in your code. Telling the model about it makes its planning realistic, but the enforcement is the for statement.
Defining Tools Well: The Description Is the Interface
The schema tells the model the shape of a call. The description tells it when to make one. Most tool misuse comes from a description that describes the shape and not the boundary.
The four parts of a tool definition
name a verb and a noun, specific enough to distinguish it from its neighbours description what it does, when to use it, AND when not to parameters a JSON schema with types, required fields and enumerations returns a one-line statement of the shape of the result string
The two parts teams leave out are the "when not to" and the returns. Both matter.
The boundary rule
If two tools could plausibly handle the same request, their descriptions must say which one wins. Consider search_docs and search_tickets. A question about a bug report is both a document question and a ticket question. Without a stated boundary the model chooses by vocabulary, which is effectively at random.
The fix is one sentence in each description:
search_docs Use for product documentation and reference material. Do NOT use for customer-reported problems; use search_tickets. search_tickets Use for customer-reported problems and their history. Do NOT use for product documentation; use search_docs.
That single pair of sentences removes a class of failure that otherwise looks like a model quality problem and is actually a specification problem.
What a result string should carry
A good result string answers four questions in one line or a short block:
status did the operation succeed data the value the model needs context anything that qualifies the value, such as a unit or a timestamp next step what the model may do if this was not enough, when that is not obvious
A weak result: {"temp": 4}. The model does not know the unit, when it was measured, or whether to try another source.
A strong result: Weather for Oslo at 08:40 UTC: 4 C, light rain, wind 12 km/h. Source: station 01492. If you need a different city, call get_weather again with the city name.
The second one costs a few more tokens per call and it removes entire categories of follow-up failures.

Argument Validation and Repair
Validation has two jobs, and they are different.
Rejecting invalid arguments protects your system. It is a security and correctness boundary.
Describing the rejection usefully protects the run. It gives the model the information it needs to try again correctly.
A validator that returns Error: invalid arguments does the first job and fails the second. A validator that returns this does both:
Error: invalid arguments for create_ticket. 'priority' must be one of [low, normal, high, critical]; received 'urgent'. 'title' is required and was not provided. Call create_ticket again with corrected values.
The model can act on that message in one turn. The alternative produces a loop that retries the same call with the same arguments, which is the most common form of a stuck agent.
Validate as a boundary, not as a formality
Treat the arguments as untrusted input, because that is exactly what they are. A model can produce a value that is syntactically valid and semantically dangerous: a path that escapes the intended directory, a SQL fragment in a search string, a user identifier belonging to somebody else.
Three rules follow:
Allow-list rather than deny-list. Enumerate the acceptable values where you can. A deny-list is a bet that you thought of every bad input, and it is a bet you will eventually lose.
Never interpolate arguments into a command, a query or a path. Pass them as parameters to a function that understands them.
Check authorisation at the tool, not at the model. The model decides what to ask for. Your code decides whether the caller is allowed to have it. The two are not the same and the second is not optional.
The repair budget
Allow one corrective retry for a validation failure, then stop. Two properties make this safe:
The retry carries the specific error, so it is a genuine correction attempt rather than a repeat.
The cap prevents a malformed request from becoming an unbounded loop.
Track the retry rate. A retry rate above a few percent means the schema or the description is unclear, and the fix belongs in the tool definition rather than in the loop.
Multi-Tool Orchestration: Parallel, Sequential and the Choice Between Them
When a task needs more than one tool, the ordering matters and so does the failure behaviour.
Parallel calls. Independent calls should go together in one turn. Three lookups for three different cities do not depend on each other, so the model can emit all three calls and the client can run them concurrently. Two conditions must hold: the calls genuinely do not depend on each other, and the tools are safe to run concurrently, which is not automatic if they write to the same resource. Three sequential calls at one second each is three seconds; three parallel calls is about one.
Sequential calls. A call whose arguments depend on a previous result must be sequential. The design question is whether the model should orchestrate the sequence or whether your code should. For a fixed sequence, put it in code as a single composite tool: the model gets one decision instead of three, and the sequence becomes testable without a model in the loop. Composite tools are one of the most under-used techniques in agent design. A common look-up-then-validate-then-write flow is far more reliable as one tool than as three the model must order correctly.
Partial failure. When some calls in a batch succeed and others fail, the result block must say which is which. A single Error for a batch of three hides the two that worked, and the model will often redo all three. Return per-call results with a stable identifier for each.
The rule that prevents most orchestration bugs. Design the tools so that the correct sequence is obvious, and so that an incorrect sequence fails loudly rather than producing a plausible wrong result. A tool that silently accepts a call with a stale or missing prerequisite turns an ordering mistake into a data problem, which is much harder to diagnose.
State, Memory and Idempotency
An agent that can take an action must be safe to retry, because retries are normal: a timeout, a dropped connection, a repair turn.
Idempotency is a design requirement, not a nice property. If a tool creates a record, a retry must not create a second one. The standard solution is an idempotency key: the caller supplies a key with the request, and the server treats two requests with the same key as one. Where the tool calls an upstream API, pass a key through. Where it writes to your own store, derive the key from the content of the request so an identical retry is recognised. Without it, a charge is applied twice, a ticket is created twice, a message is sent twice, and none of those appear in a test that runs each tool once.
What belongs in memory. An agent's state is the message list, and it grows. Three things belong in it: the task, the results the model needs, and the decisions already made. For a long run, keep the recent turns verbatim and summarise the older ones, exactly as you would for a conversation. The summary must preserve the decisions, not the data: what was tried and what was concluded is what matters after ten turns.
The run record. Log enough to replay a run without the model, with five fields per step:
run_id identifies the whole run turn the position in the loop tool_call the name and the arguments the model produced tool_result the string the tool returned decision whether the loop continued and why
With those five, a failing run is reconstructed exactly: the same calls replayed against the same tools produce the same decisions. Without them, an agent failure is an anecdote. Store the record on success too: the successful runs are how you learn what normal looks like.

Human in the Loop: Where to Put the Gate, and How
Autonomy is a dial, not a switch. The dial position is decided by the cost of being wrong, and the correct design places the human where that cost is highest.
Before the action. The agent proposes, a person approves, then the agent executes. The right setting where the action is irreversible or externally visible: sending a message, issuing a refund, deleting data.
After the draft. The agent produces the complete result, a person reviews it, then it is released. The right setting for anything a customer will read.
On an anomaly only. The agent runs unattended and escalates when a condition triggers: low confidence, a value outside a normal range, a repeated failure, a request touching a sensitive category. The right setting for high-volume work where most cases are routine.
An approval gate is only useful if approving is cheap. Show the action, the exact arguments and the consequence on one screen; make approval a single action; capture a reason on rejection, because that reason is your best signal for the next prompt revision. A gate that requires a reviewer to reconstruct the context will be bypassed under load.
Write the escalation conditions into the prompt and enforce them in code, because a prompt cannot guarantee a condition is respected. The conditions worth naming: the task requires an action outside the allow-list, the agent has failed the same step twice, the request is ambiguous in a way that changes the correct action, the request touches a category your policy marks sensitive. An agent that escalates correctly is worth more than one that succeeds more often, because the escalations are where a wrong action was most expensive.
Track three numbers for any gate: the proposal rate, the approval rate and the time to approve. A falling approval rate means worse proposals. A rising time to approve means the queue is the bottleneck, which is the point at which a team is tempted to remove the gate. Make that decision on the numbers, and count what the gate caught.
Testing an Agent: What to Assert and What to Fake
An agent is harder to test than a function because the model is non-deterministic. Test the parts that are deterministic and constrain the parts that are not.
Test the tools directly. Every tool is a function. Test the arguments it accepts, the arguments it rejects, the string it returns for each outcome, and the timeout behaviour. This ordinary unit testing covers the majority of an agent's failure surface, because most agent failures are tool failures.
Test the loop with a stub model. Replace the model with a stub that returns a scripted sequence of calls and a final message. The loop is now deterministic and you can assert that an unknown tool produces a refusal naming the valid tools, that invalid arguments produce a message naming the specific problem, that a timeout produces a string rather than an exception, that the budget stop is clear, and that a successful run produces the expected transcript. This suite runs in milliseconds and never calls a model.
Test the live model on a fixed scenario set, asserting properties rather than exact text: the correct tool was used at least once, no forbidden tool was used, the final answer contains the required information, the run finished inside the budget. Properties survive model updates; exact-text assertions break on every model change and get deleted within a month. Do not assert a particular call sequence when several sequences are valid.
The regression set. Every production failure becomes a scenario, added with the property it violated, and run before any change to a prompt, a tool description or a model version. That is the same discipline as a labelled evaluation set for retrieval, with the same property: each failure becomes a permanent check.
A Complete Worked Agent: Research and Summarise
The following is a small but complete agent, chosen so that every part of the design is visible.
The task
"Given a company name, find its most recent funding announcement and return a two-sentence summary with the amount and the date."
The tools
search_news(company, since) returns the top matches with titles and snippets fetch_page(url) returns the readable text of a page, truncated record_summary(company, text) stores the summary and returns its identifier
Three tools, each with a stated boundary. search_news is for finding; fetch_page is for reading; record_summary is for writing. None of them can do another's job.
The loop configuration
MAX_TURNS 8 per-tool timeout 15 seconds validation on every argument, enum on `since`
Eight turns is generous for a three-tool task and it is a hard cap, not a suggestion.
What the transcript looks like
turn 1 tool_call search_news(company="Northwind", since="2026-01-01") tool_result 3 results with titles, dates and snippets turn 2 tool_call fetch_page(url="https://example.com/announcement") tool_result 4,200 characters of readable text, truncated at 4,000 turn 3 tool_call record_summary(company="Northwind", text="Northwind raised 40 million USD in a Series B on 2026-03-11 ...") tool_result summary recorded with id sum_8821 turn 4 final "Northwind raised 40 million USD in a Series B on 2026-03-11. The round was led by ..."
Four turns, well inside the budget. If it had taken seven, the right response would be to look at why, not to raise the cap.
The failure paths, exercised
search_news returns nothing. The tool returns a string saying no results were found for the company and the date range, and the agent's correct behaviour is to report that rather than to invent an announcement.
fetch_page times out. The tool returns a timeout string, and the model should try a different URL from the search results. Two timeouts in a row and the loop should stop and report.
record_summary receives a summary that is longer than the allowed length. The validation layer rejects it with the limit stated, and the model shortens it.
The model calls fetch_page with a URL that is not in the search results. The tool's description states that URLs must come from search_news, and the validation layer enforces it.
Each of those paths is a test in the stub-model suite, and each one is a real defect that would otherwise reach production.
The measurements to keep
completion rate the share of runs that produced a final answer median turns how much work the task actually takes tool error rate per tool, which tool is unreliable validation rejection rate per tool, which schema is unclear escalation rate how often a person was needed cost per run in tokens, not in dollars, so it survives a price change
Six numbers. They tell you which tool to fix, when the budget is too tight, and whether the agent is improving.
The reader exercise
Add a fourth tool to the worked agent: check_duplicate(company) that returns whether a summary for this company already exists. Then write the loop rule that uses it. The interesting part of the exercise is deciding where the check belongs: a tool the model may call, or a step your code runs before the loop starts. Both are defensible, and the argument between them is the design conversation this lecture is about.
Feynman Summary: Explain It Like You Are 12
Imagine you have a smart friend who knows a lot but cannot leave their room. That friend is the language model. You are outside the room. Your friend is the brain, and you are the hands.
Function calling is when your friend slides a note under the door that says "Please check the weather in Paris and tell me what you find." You go check the weather (using your phone, a weather app, or looking outside), write the answer on the note, and slide it back under the door.
Your friend reads the answer and says "The weather in Paris is 18 degrees and partly cloudy." That is the agent answering the user.
The agent loop is when your friend sends multiple notes. First: "Check the weather in Paris." You check and slide the answer back. Then: "Now check London." You check and slide it back. Finally your friend says "Paris is warmer than London by 3 degrees." The loop continues until your friend has enough information to answer.
Multiple tool calls is when your friend sends three notes at once: "Check Paris, London, and Tokyo." You check all three at the same time and slide all three answers back at once. Your friend then compares them.
The big idea: the model is the brain that decides what to do. Your code is the hands that actually do it. Function calling is the note under the door that connects them.
Mindmap: The Complete Picture

This mindmap shows the full agent architecture: the agent loop at the center, tool definitions, the four-step round trip, error handling patterns, production considerations, and multi-agent workflows branching out from the core.

UNOP Sound (University 365 Neuroscience Oriented Pedagogy)
Take five minutes to consolidate your memory. Play the isochronous tone track (10Hz alpha frequency) with your eyes closed. Alpha-frequency tones after a learning session support consolidation, helping move what you just learned from short-term to long-term memory.
[Audio player: UNOP Post-Lecture Isochrone (10Hz, 5 minutes)]
Practical Exercise: Build a Calculator Agent
Build a simple agent that can perform calculations. You will need Python and the openai package (or any LLM SDK that supports function calling).
Step 1: Define the Calculator Tool
Create a Python function that takes a mathematical expression string and returns the result. Use a safe evaluation method (not eval) to compute the result. The function should handle basic arithmetic: addition, subtraction, multiplication, division.
Step 2: Define the JSON Schema
Write the tool definition with name "calculate", a description "Perform a mathematical calculation", and parameters for the expression string.
Step 3: Build the Agent Loop
Write the loop that sends messages to the model, checks for tool calls, executes the calculator, and returns results. Set a maximum of 5 turns.
Step 4: Test with These Questions
- "What is 15 times 23?"
- "Calculate the sum of all numbers from 1 to 100"
- "If I have 3 boxes with 12 items each, how many items do I have?"
Step 5: Add a Second Tool
Add a "get_current_time" tool that returns the current time. Test: "What time is it, and what is 50 plus 25?" The agent should call both tools and combine the answers.
This exercise gives you hands-on experience with the complete agent loop, tool definition, and multi-tool coordination.
Glossary
Term | Definition |
Function Calling | Mechanism where a language model requests execution of external functions by returning structured JSON with function name and arguments. Also called tool use. |
Tool Use | Synonym for function calling. The model uses tools (functions) to interact with external systems. |
Agent Loop | The cycle of sending a message to the model, checking for tool calls, executing them, and returning results. Repeats until the model gives a final answer. |
Tool Schema | JSON object describing a function: its name, description, and parameter types. The model uses this to decide when and how to call the function. |
JSON Schema | Standard for describing JSON data structures. Used in tool definitions to specify expected parameters and their types. |
Tool Map | Dictionary mapping tool names to their Python function implementations. Used to look up and execute the correct function. |
Parallel Tool Calls | When the model requests multiple tool calls in a single response. Your code can execute them concurrently to reduce latency. |
Sequential Tool Calls | When the model requests tool calls one after another, where the result of one determines the arguments of the next. |
Maximum Turns | Safety limit on the number of agent loop iterations. Prevents infinite loops when the model is confused. Typically 5 to 10. |
Human in the Loop | Pattern where human approval is required before executing tools with side effects (payments, emails, deletions). |
Structured Output | Feature that forces the model to return output in a specific JSON format, eliminating parsing failures. |
Agent Framework | Library that provides abstractions for building agents: LangChain, CrewAI, AutoGen, OpenAI Assistants. All built on function calling. |
Multi-Agent System | Architecture where multiple agents with different roles (research, planning, execution, review) collaborate on complex tasks. |
Handoff | When one agent passes a task to another agent through a structured message or function call. |
Tracing | Recording every tool call, argument, result, and model reasoning for debugging and observability. |
Rate Limiting | Controlling the rate at which tools are executed to prevent overwhelming external APIs. |
Token | Unit of text processed by the model. Tool definitions consume tokens in the context window. |
UNOP | University 365 Neuroscience-Oriented Pedagogy: the teaching framework behind this lecture format. |
Quiz: TEST YOUR UNDERSTANDING
1. What is the key difference between a chatbot and an AI agent?
A) Agents use larger models than chatbots
B) Agents can call external functions to act on the real world; chatbots only generate text
C) Agents are faster than chatbots
D) Agents do not use language models
2. Who executes the function when the model requests a tool call?
A) The language model executes it directly
B) The API provider executes it
C) Your application code executes it and returns the result
D) The operating system executes it automatically
3. Why does the agent loop need a maximum turn limit?
A) To reduce API costs
B) To prevent infinite loops when the model is confused
C) To comply with API rate limits
D) Both A and B
4. What should you do when a tool execution fails in production?
A) Raise an exception and crash the agent
B) Return the error as a string so the model can retry or apologize
C) Ignore the error and continue
D) Restart the entire agent loop
5. When the model returns multiple tool calls in one response, what should you do?
A) Execute only the first one and ignore the rest
B) Execute them one at a time, sequentially
C) Execute all of them and append all results before sending back to the model
D) Ask the user which one to execute first
Answers: 1-B, 2-C, 3-D, 4-B, 5-C
Related Resources
U365 INSIDE Publications
- How LLMs Actually Work: Transformers in 20 Minutes (AI Foundations, Lecture 1)
- RAG vs Fine-Tuning: When to Use Each (AI Engineering, Lecture 2)
- Prompt Engineering at Production Scale (AI Skills, Lecture 5, coming soon)
External Resources
- OpenAI Function Calling Guide: official documentation for tool use with GPT models
- Anthropic Claude Tool Use: function calling with Claude models
- Google Gemini Function Calling: tool use with Gemini models
- LangChain Agents documentation: framework for building multi-tool agents
- CrewAI: multi-agent framework with role-based design
- AutoGen: Microsoft's multi-agent conversation framework
Related U365 Lectures (Coming Soon)
- Vector Databases Explained: Embeddings for Search (AI Engineering, Lecture 4)
- The AI Stack 2026: What Every Developer Needs (AI Engineering, Lecture 10)
- AI Safety and Alignment: Why Hallucinations Happen (AI Foundations, Lecture 8)
U.Copilot for This Lecture
Copy and paste this prompt into the U.Copilot AI Agent on university-365.com to explore this topic further:
I just completed the U365 INSIDE Lecture "Building Your First AI Agent with Function Calling" from UIT. I want to build my own agent. Help me: 1. Identify 3 tools my agent would need for my use case 2. Write the JSON schema for each tool 3. Design the agent loop (what tools, what order, max turns) 4. Suggest error handling patterns for my specific tools 5. Recommend whether I need a single agent or a multi-agent system My use case is: [describe your project here]
Next Steps
1. Take the quiz above and check your answers at the bottom of this section.
2. Complete the Practical Exercise: build a calculator agent with a second tool.
3. Read the next lecture in the AI Agents series: multi-agent workflows (coming soon).
4. If you have not completed Lecture 1 (How LLMs Actually Work), start there for foundational knowledge.
5. Visit university-365.com/uit to explore UIT programs in AI Engineering and Software Development.
6. Try the U.Copilot prompt above to design an agent for your own project.
IMPORTANT NOTICE
Copyright University 365, Inc. All rights reserved.
This lecture is part of the U365 INSIDE Lectures series, produced by UIT (University 365 Institute of Technology) under the UDA Department of Academics. The content follows the UNOP (University 365 Neuroscience-Oriented Pedagogy) framework and the 5M2S (5 Minutes to Success) microlearning format.
All lectures in this series are free to access. For enrollment in UIT degree programs, certificate programs, or executive education, visit university-365.com/tuition.
For permissions or inquiries, contact uda@university-365.com.
This content is for educational purposes. Code examples are illustrative and may require adaptation for production use. Always consult official API documentation for current function calling formats, as provider APIs evolve.
Published by the Department of Academics, University 365.
Lecture delivered by the University 365 Institute of Technology (UIT).
Sam Utteker, Dean of Technology, UIT
Signed for the academic year 2026.









Comments