Let's build an LLM agent in Python, step-by-step โ†“๐Ÿงต

@paulabartabajo_
Pau Labarta Bajo@paulabartabajo_
23 views May 04, 2025 ~3 min read
Advertisement
1
Let's build an LLM agent in Python, step-by-step โ†“๐Ÿงต
2
Why agents ๐Ÿค–โ“

Because Large Language Models alone are not enough to accurately answer complex tasks that require

-> External information that was not present in the training dataset used to fit the LLM paramaters
or
-> Many reasoning steps
3
For example, if you ask OpenAI gpt-3.5-turbo LLM to

"Create a plot with Python of the number of games won by the Golden State Warriors in each of the last 2 NBA seasons."

The response you get is
4
โ†’ a perfectly valid Python code snippet, because GPT-3.5 was trained on vast amounts of Python code available on the Internet, and hence can work as a great code generator tool.

โ†’ BUT, the data plotted is wrong, as it is not from the last 2 seasons, but from 2019 and 2020.
Media image
5
To fix this you need to supercharge your LLM with

โ†’ A tool to retrieve external data needed for the task

and

โ†’ The reasoning capabilities to decide when and how to use the tool, to answer the user query.

And this is what agents are all about.
6
Let me show you how to build a ReAct (Reason and Act) agent in Python that can generate the plot we want.

All the source code is in this repository โ†“โ†“โ†“
github.com/Paulescu/plot-โ€ฆ
7
Step 1. Pick your LLM

You can either use

โ†’ a local LLM running on your laptop with @ollama, or
โ†’ connect to an external API like @OpenAI or @cohere.
Media image
8
@ollama @OpenAI @cohere Step 2. Define your tools ๐Ÿ› ๏ธ

In this case we need:

1) An Internet search tool
Media image
9
@ollama @OpenAI @cohere 2) A PythonREPL so the LLM can run Python code to debug its output
Media image
10
@ollama @OpenAI @cohere Step 3. Build the ReAct agent from the LLM you picked and the tools

This step is super-simple thanks to a library like @langchain, which provides an implementation of the ReAct agent logic.

react-lm.github.io
11
@ollama @OpenAI @cohere @langchain We first define the agent logic, using the

> tools we created
> the base LLM we picked, and
> an initial prompt of that can help us steer the the agent in the right direction
Media image
12
@ollama @OpenAI @cohere @langchain From this agent logic, we define the AgentExecutor, which is the runtime for an agent, that

> calls the agent
> executes the actions it chooses
> passes the action outputs back to the agent
> and repeats
Media image
13
@ollama @OpenAI @cohere @langchain Step 4. Run the agent ๐Ÿƒ

We pass the input query

"Create a plot with Python of the number of games won by the Golden State Warriors in each of the last 2 NBA seasons."

to the agent_executor
Media image
14
@ollama @OpenAI @cohere @langchain ...and the agent start reasoning and acting.

Until it produces the correct plot!
Media image
15
@ollama @OpenAI @cohere @langchain As you start experimenting with different input prompts, you will find scenarios in which the agent is not able to solve the task.

Moving from agent prototypes to production-ready agents is all about polishing rough edges, and ensuring the agent has access to the right tools.
16
@ollama @OpenAI @cohere @langchain But this is something we will leave for another day.
17
Join 22k+ ML builders to the ๐—ฅ๐—ฒ๐—ฎ๐—น-๐—ช๐—ผ๐—ฟ๐—น๐—ฑ ๐— ๐—Ÿ ๐—ก๐—ฒ๐˜„๐˜€๐—น๐—ฒ๐˜๐˜๐—ฒ๐—ฟ, and learn to build Real-World ML Products.

๐—˜๐˜ƒ๐—ฒ๐—ฟ๐˜† ๐—ฆ๐—ฎ๐˜๐˜‚๐—ฟ๐—ฑ๐—ฎ๐˜† you will get an email with hands-on content to build real-world ML apps.
โ†“โ†“โ†“
paulabartabajo.substack.com
Actions
What You Can Do
  • Export as PDF or Markdown
  • Batch Export to Notion
  • Bookmark & Highlight
  • Screenshot Tweet
Create Free Account

Includes 7-day Premium trial

Advertisement