Python AI Agent: How to Build an AI Agent in Python, and the Best Libraries (2026)
Python for AI agents in November 2026: a minimal agent loop in plain Python, the best Python libraries (OpenAI Agents SDK, Claude Agent SDK, Pydantic AI, LangGraph, CrewAI, Google ADK, smolagents) compared, how to deploy it, and when SPIRITT writes and runs the Python for you.
Python is the most common language for building AI agents in 2026, because every major model provider and agent library ships a Python SDK first. A Python AI agent is a program that sends a goal to a language model, lets the model choose tools (Python functions or APIs), runs them, feeds the results back and loops until the task is done. The best Python libraries for AI agents are the OpenAI Agents SDK, the Claude Agent SDK, Pydantic AI, LangGraph, CrewAI, Google ADK and Hugging Face smolagents. SPIRITT, the operating system for AI transformation, is the shortcut when you want the agent running rather than a codebase to maintain: it writes the Python (or any language), tests it, deploys it on its own cloud computer and runs it on a schedule across 1,500+ apps, together with your sales, ops and admin work. This guide shows the core loop, compares the libraries, and covers deployment, testing and cost.
How a Python AI agent works
Every agent library wraps the same loop:
- Goal and instructions. You give the model a task and a system prompt that describes its role and limits.
- Tools. You describe Python functions (search, read a file, call an API, query a database) with names, arguments and docstrings.
- Decide. The model returns either a final answer or a tool call with arguments.
- Act. Your code runs the tool and returns the result to the model.
- Repeat until the model returns a final answer or hits a step, time or cost limit.
A minimal version in plain Python fits in about 30 lines with any provider's SDK: a list of messages, a dictionary of tool functions, and a while loop that dispatches tool calls. Libraries add the parts that take longer to get right: structured outputs, retries, memory, multi-agent handoffs, tracing and guardrails.
Best Python libraries for AI agents (checked November 2026)
| Library | Style | Best for | Models | Price |
|---|---|---|---|---|
| OpenAI Agents SDK | Few primitives: agents, handoffs, guardrails, sessions, tracing | Fast start, multi-agent handoffs, voice agents | OpenAI first; others through LiteLLM and Any-LLM | Free, open source |
| Claude Agent SDK | Claude Code's agent loop as a library, with file, shell and web tools | Agents that read and edit files, run commands and write code | Claude | Free SDK; Claude API usage |
| Pydantic AI | Typed agents with validated, structured outputs | Production Python apps that need type safety | Every major provider | Free, open source |
| LangGraph | Stateful graphs with checkpoints and human approval | Complex, long-running flows you need to control step by step | Any | Open source; LangSmith Plus $39 per seat a month for hosting and tracing |
| CrewAI | Role-based crews of agents | Quick multi-agent prototypes with roles | Any | Open source; hosted Basic free (50 executions a month) |
| Google ADK | Graph, sequential, parallel and loop workflows | Gemini and Google Cloud teams | Gemini and others | Free, open source |
| smolagents | Agents that write and run Python code as their actions | Research and code-centric agents | Any, including open models | Free, open source |
| SPIRITT | Plain English; SPIRITT writes and runs the code | Teams that want the agent deployed and running | Composes models from several labs within each task | Free to start; Starter $32, Pro $256, Max $1,024 a month |
How to pick one
- Want the smallest learning curve? OpenAI Agents SDK.
- Want typed, validated outputs? Pydantic AI.
- Need durable, multi-step flows with approvals? LangGraph.
- Building a coding or file-heavy agent? Claude Agent SDK.
- On Google Cloud? ADK.
- Prefer agents that act by writing code? smolagents.
For a deeper comparison of the vendor SDKs, see AI agent SDKs compared, and for the architecture choices behind them, AI agent design patterns.
Build a Python AI agent: a practical path
- Start with one job. For example: "every morning, compare yesterday's Stripe payouts with QuickBooks and post mismatches to Slack."
- Write the tools as plain functions. fetch_payouts(date), fetch_ledger(date), post_to_slack(message). Keep each one small, typed and documented; the docstring is what the model reads.
- Pick a library and wire the loop. In Pydantic AI or the OpenAI Agents SDK this is an agent object with instructions and a list of tools.
- Return structured output. Ask for a typed result (a list of mismatches with amounts and IDs) so your code can check it.
- Add limits. Maximum steps, a timeout, a cost cap, and approval before anything that sends money or messages outside the company.
- Test on real history. Run it on the last 30 days and compare with what your team found by hand. Our guide to AI agent evaluation shows how to turn that into a repeatable eval.
- Deploy and schedule it. A container or serverless function, a scheduler, secrets for each API, logs and alerts.
Deploying a Python agent: the part tutorials skip
Running the script on your laptop is the easy part. In production the agent needs a host that stays up, OAuth tokens that refresh, secrets that agents never see in plain text, a sandbox if it browses or runs code, traces of every run, and someone to fix it when an API changes.
That is the gap SPIRITT fills. SPIRITT composes models from several labs within each task, so each step uses a model that fits it. You can describe the agent in plain English and SPIRITT writes the Python, writes the tests, deploys it on its own isolated cloud computer and runs it on a schedule or on events such as a new lead or a failed payment. You can also bring your own repository: SPIRITT works in GitHub repos, runs coding agents such as Claude Code and Codex in the same place, and can host agents built with any of the libraries above. Keys are injected at the network boundary, every integration has allow, ask and never rules, and budgets per agent and task stop runaway costs.
Examples of Python agents teams run this way:
- Payout reconciliation between Stripe and QuickBooks, with mismatches posted to Slack.
- Lead enrichment that researches each new HubSpot contact and writes a short brief.
- Data pipeline checks that compare row counts and freshness across warehouses and alert the owner.
- Repo maintenance that triages GitHub issues, reproduces bugs and opens pull requests with tests.
- Web monitoring that watches competitor pricing pages and sends a weekly digest.
FAQ
Is Python good for AI agents?
Yes. Python has first-party SDKs from OpenAI, Anthropic and Google, the largest set of agent libraries, and mature tools for data, APIs and testing, which makes it the default language for AI agents.
What is the best Python library for AI agents?
It depends on the job: the OpenAI Agents SDK for a fast start, Pydantic AI for typed outputs, LangGraph for complex stateful flows, the Claude Agent SDK for file and code work, CrewAI for role-based teams, and ADK on Google Cloud.
What is a Pydantic AI agent?
An agent built with Pydantic AI, the Python framework from the Pydantic team. You define instructions, tools and a typed output model, and the framework validates every result and works with every major model provider.
How do I build an AI agent in Python?
Define tools as Python functions, choose a library or write a short loop around a model API, return structured output, add limits and approvals, test it on real history, then deploy and schedule it.
Can I build a Python AI agent without coding?
Yes. Describe what the agent should do to SPIRITT in plain English; it writes the Python, tests it, deploys it and runs it, and you can read or change the code at any time.
How much does it cost to run a Python AI agent?
The libraries are free. You pay for model tokens, hosting and any paid APIs. Cost depends on how many steps each run takes and which models it uses; set a cost cap per run from the start. See AI agent development cost for full project budgets.
What Python libraries do AI agents need besides the framework?
Usually an HTTP client, the SDKs for the apps you connect, a database or vector store for memory, a scheduler, and tracing. Platforms like SPIRITT include integrations, scheduling and audit trails so you write less of this yourself.
How we checked
SPIRITT publishes this guide. Library facts come from the official documentation of the OpenAI Agents SDK, the Claude Agent SDK, Pydantic AI, LangGraph, CrewAI, Google ADK and smolagents, checked in November 2026; LangSmith and CrewAI prices come from their pricing pages in November 2026. SPIRITT facts come from spiritt.ai and the SPIRITT product.