PYTHON + LLMS
Python AI Agents Tutorials
An AI agent is a language model that can call your Python functions, look at the results and keep going until the job is done. Start here to learn how agents work and how to build one.
- 94 tutorials
- 14 topic areas
- 3.12 Python tested
What is a Python AI agent?
A Python AI agent is a large language model (LLM) that can call functions you write in Python, read what they return, and decide what to do next. It repeats that loop until it has a final answer or hits a step limit you set.
A chatbot only replies with text. A normal script follows the exact steps you coded. An agent sits in between: you supply the tools, and the model picks which ones to call, in what order, with what arguments.
The agent loop is simple. You send the question and a list of tools to the model. If it asks for a tool, your code runs that function and sends the result back. Then you call the model again. No tool call means a final answer.
Tools are plain Python functions plus a short JSON description: a name, what the tool does and its parameters. The model never runs your code. It only asks, and your program decides whether to run it.
Memory is the message history you pass in on each call. Short-term memory is that list. Long-term memory means saving facts to a file or database, or pulling relevant documents in with retrieval (RAG).
Guardrails keep an agent safe and affordable: a max-steps cap, input validation on every tool, read-only tools where possible, and a human approval step before anything that sends, pays or deletes.
When I build agents for clients, the most common mistake I see is using one where a plain function would do. If the steps never change, write a script. It is faster, cheaper and easier to test.
Set up Python for AI agents
You need Python 3.10 or newer, an OpenAI API key and two packages. Create a virtual environment so the agent’s packages stay separate from everything else on your machine.
Put the key in a .env file next to your script, never in the code itself. python-dotenv loads it, and the openai client reads it automatically.
Your .env file holds one line, OPENAI_API_KEY=your-key-here. Add .env to .gitignore so the key never reaches GitHub.
Your learning path
Learn Python AI agents in this order
Each stage builds on the one before it. If you have never called an LLM from Python, start at step 1.
- 1
Build your first agent
Write the agent loop by hand with two real tools and a step limit.
- 2
Learn the OpenAI API
Make calls, read responses and handle errors in Python.
- 3
Add tools and structured outputs
Describe tools with JSON Schema and get typed data back.
- 4
Add RAG and memory
Ground answers in your own documents with embeddings.
- 5
Use a framework and deploy
Compare LangGraph, Pydantic AI, CrewAI and others, then ship it.
Quick reference
The Python agent loop at a glance
The pieces you will write in almost every agent.
| Task | Code | Worth knowing |
|---|---|---|
| Call the model | client.responses.create(...) | One request, one response. |
| Describe a tool | {"type": "function", "name": ...} | Name, description and a JSON Schema. |
| Spot a tool call | item.type == "function_call" | No calls means a final answer. |
| Send a result back | {"type": "function_call_output"} | Include the matching call_id. |
| Stop runaway loops | for step in range(1, max_steps + 1) | Always cap the steps. |
| Load the API key | load_dotenv() | Keeps the key out of your code. |
Pick the right tool
Chatbot, script or AI agent?
An agent is not always the answer. Here is how the three compare.
| Chatbot | Python script | AI agent | |
|---|---|---|---|
| Who decides the steps | Nobody, it only replies | You, in code | The model, at run time |
| Can run your functions | No | Yes, always the same ones | Yes, the ones it picks |
| Handles new requests | Talks about them | Only what you coded | Combines tools to solve them |
| Cost and speed | One model call | No model calls | Several model calls |
| Best for | Q&A and drafting | Fixed, repeatable jobs | Open-ended, multi-step tasks |
Every tutorial, by topic
Python AI agent tutorials by topic
Linked titles are ready to read. Grey titles are on the way and will be linked as they go live.
Getting Started
4Your first agent, the OpenAI API and the core ideas.
Building Agents
11Tools, memory, planning and multi-step agent loops.
OpenAI API
5Calls, streaming, function calling and the Responses API.
Pydantic and Structured Outputs
5Get clean, validated data back from a model.
AI Agent Projects
11Complete agents you can build, run and adapt.
Documents, RAG and Data
9Ground answers in your own files, databases and embeddings.
Claude, Gemini and Other LLMs
12Use other providers and local models from Python.
LangChain
7Chains, tools and agents with LangChain.
LangGraph
7Stateful, graph-based agents with LangGraph.
Safety and Security
4Guardrails, prompt injection and safe tool use.
Testing, Evaluation and Observability
5Test agents, score their answers and trace every step.
Reliability and Performance
5Retries, rate limits, caching and cost control.
Deploy and UI
3Put an agent behind an API or a simple web UI.
Integrations and MCP
6Connect agents to MCP servers and outside services.
Keep going
More Python learning
Related hubs for what comes next.
Questions people ask
Frequently asked questions
What is an AI agent in Python?
It is a Python program that sends a task and a list of tools to an LLM, runs the tools the model asks for, and returns the results until the model gives a final answer.
Do I need a framework like LangChain to build an AI agent?
No. A working agent is about 40 lines with the openai package. I suggest writing one by hand first, then moving to LangGraph or Pydantic AI when you need state, retries or many agents.
What is the difference between an AI agent and a chatbot?
A chatbot only produces text. An agent can take actions through tools, such as looking up an order or querying a database, and it decides on its own which steps to take.
Can I run a Python AI agent without an API key?
Yes. Ollama runs open models on your own computer, and several of them support tool calling. Expect slower answers and weaker tool use than the hosted models.
How do I stop an AI agent from looping forever?
Cap the loop with a max_steps value, return a clear message when the cap is hit, and log every tool call so you can see where it got stuck.
Where can I learn more about function calling?
Start with the tutorials above, then read OpenAI’s function calling guide for the full list of options.
Ready to build your first AI agent?
Start with a single loop and two tools, then add memory, RAG and a framework one step at a time.