
How-Tos
How to build a first LlamaIndex FunctionAgent
Install LlamaIndex, give a FunctionAgent add and multiply tools, and run one async math question from the official tutorial.
Searcher → Analyst → Writer → Editor · subagentic-20261028-0800
LlamaIndex describes an agent as a semi-autonomous piece of software powered by an LLM. You give it a task and a set of tools. It selects a tool for each step. A tool can be an arbitrary function or a full LlamaIndex query engine. When a step finishes, the agent judges whether the task is complete. If it is, the agent returns a result to the user. If it is not, the agent loops back and takes another step.
You can build your own agentic workflows from scratch, or you can use a pre-built one. The official Building an agent page covers FunctionAgent, a simple function-calling agent. The exercise is small on purpose: a virtual environment, two typed tools, one model, one math question, and the asyncio wrapper a script needs. The page also points at multi-agent reading elsewhere. This walkthrough does not go there.
Start the environment the tutorial starts
To avoid conflicts, the guide starts a new Python virtual environment. Any manager works. The commands it shows use Poetry:
poetry init
poetry shell
Then it installs LlamaIndex and the other dependencies used in the example:
pip install llama-index-core llama-index-llms-openai python-dotenv
Those are the only install packages on the page. No versions, extras, or index URLs are specified. If the install gives you trouble, the tutorial sends you to its installation guide rather than listing a fallback command. The same getting-started section says all of this code is in the agents tutorial repo, which is the check against your own file once the script runs.
Add the OpenAI key
The agent is powered by OpenAI’s gpt-4o-mini, so you need an API key. After you have one, the page puts it in a .env file in the root of the project:
OPENAI_API_KEY=sk-proj-xxxx
The value on that line is the placeholder in the documentation. Replace it with your key. The tutorial does not show a different variable name.
If you do not want OpenAI, the page says you can use any other LLM, including local models. It also says agents require capable models, so smaller models may be less reliable. The rest of this article follows the documented OpenAI constructor and does not add setup steps for those other models.
Bring in the dependencies
The tutorial loads environment variables first, then imports the LLM class and FunctionAgent:
from dotenv import load_dotenv
load_dotenv()
from llama_index.llms.openai import OpenAI
from llama_index.core.agent.workflow import FunctionAgent
load_dotenv() reads the .env file. OpenAI is imported from llama_index.llms.openai. FunctionAgent is imported from llama_index.core.agent.workflow.
Create the multiply and add tools
The example creates two tools: one that multiplies numbers, and one that adds them. They are regular Python functions. When the agent decides what tool to use, it uses the tool’s name, parameters, and docstring to determine what the tool does and whether it is appropriate. It also uses the type hints to determine the expected parameters and return type. The page says the docstrings should be descriptive and helpful. Copy the functions as published, including those docstrings:
def multiply(a: float, b: float) -> float:
"""Multiply two numbers and returns the product"""
return a * b
def add(a: float, b: float) -> float:
"""Add two numbers and returns the sum"""
return a + b
Each function takes two float arguments and returns a float. The multiply function returns the product of its two arguments. The add function returns their sum. No other tools are defined.
Initialize the LLM and the agent
The model doing the work in the tutorial is gpt-4o-mini:
llm = OpenAI(model="gpt-4o-mini")
The page notes you could pick another popular model available through an API. It names Mistral, Claude, and Gemini as examples and links out to those integrations. It does not show their constructor calls in this tutorial, so they stay out of this script.
The agent needs an array of tools, an LLM, and a system prompt:
workflow = FunctionAgent(
tools=[multiply, add],
llm=llm,
system_prompt="You are an agent that can perform basic mathematical operations using tools.",
)
The docs assign that object to workflow. The later run call uses that name, so keep it if you are matching the sample.
The system prompt tells the model what kind of agent to be. The tutorial says your system prompt would usually be more detailed than this. It also explains the wording that is here: GPT-4o-mini is smart enough not to need tools for such simple math, which is why the prompt says to use tools.
Beyond FunctionAgent, the page names other agents, including ReActAgent and CodeActAgent, which use different prompting strategies to execute tools. This first script does not construct them.
Ask the one documented question
The question is fixed in the tutorial. In a notebook, the asynchronous call is:
response = await workflow.run(user_msg="What is 20+(2*4)?")
print(response)
That code will not run as a top-level script. The page says it works in a notebook, and that regular Python needs an asynchronous wrapper:
async def main():
response = await workflow.run(user_msg="What is 20+(2*4)?")
print(response)
if __name__ == "__main__":
import asyncio
asyncio.run(main())
The sample imports asyncio inside the main-guard block, which is how the published wrapper is written. The user message is the math question in those run calls. The tutorial does not document other arguments on that call.
The page says this should give output similar to:
The result of (20 + (2 times 4)) is 28.
The documented sample result is 28. Treat the sentence as an example of similar output, not as a string you must match exactly. The tutorial does not show intermediate tool-call logs in that sample, so printing the response is the whole check it demonstrates.
Try the sample, then stay on the same page
Put your key in .env, install the three named packages, and run either the notebook form or the asyncio wrapper shown above. Compare what prints with the sample that reports 28. After that, the tutorial’s own next step is pre-built tools, and it tells you to check the agents tutorial repo for what the final code should look like. Do not add tools, change the model string, or rewrite the question until that sample is running. If a step here is unclear, use the official Building an agent page rather than inventing a flag.