Building AI Agents in Plain Python: Can You Skip the Frameworks?
Building an AI agent does not always require a large orchestration framework. This article examines what LLM frameworks provide, when plain Python and direct model API calls offer a simpler alternative, and how to structure prompts, tool registration, execution loops, and production infrastructure for a native implementation.
Building an AI agent often starts with an architectural choice: use an open-source orchestration framework, or implement the agent directly with Python and a large language model API.
For real-world projects, the second option is entirely viable. In many business scenarios, a plain Python implementation can also be the better architectural fit. The decision depends on the complexity of the workflow and whether the framework’s abstractions solve problems the application actually has.
1. From Framework-First Development to Direct API Calls
As LLM technology evolves, attitudes toward frameworks are changing. One leading model provider recommends in its technical documentation that developers start with direct model API calls, noting that many common patterns require only a few lines of code.
The provider also warns that incorrect assumptions about a framework’s underlying behavior are a common source of technical problems. Frameworks simplify routine tasks and make it easier to get started, but they can introduce abstraction layers that obscure prompts and responses. This makes debugging harder and can encourage unnecessary complexity when a simpler implementation would suffice.
Other technology providers take different approaches. Some actively promote frameworks, some offer lightweight SDKs that emphasize code-first development and avoid excessive abstraction, and others focus on interoperability across frameworks. These differences suggest that the industry has not settled on a single preferred approach.
Engineering experience also highlights the costs of adopting frameworks without a clear need. One technology team reported removing an LLM application framework after using it in production for a year. Its central takeaway was that the team became happier and more productive after the removal.
At their core, many LLM applications rely on string processing, API calls, and control loops. Python already handles these tasks well. Additional abstractions can become obstacles when developers need to inspect behavior or customize execution.
2. What Do LLM Frameworks Actually Provide?
To understand the benefits of direct implementation, it helps to identify the problems frameworks are designed to solve. Their core abstractions typically cover four areas:
- Unified model interfaces: Adapter layers normalize differences between providers’ APIs and help reduce provider lock-in.
- Multi-step execution and state management: Chains connect multiple model calls or processing steps while maintaining state and memory throughout execution.
- Complex workflow orchestration: Graph-oriented frameworks provide nodes, conditional edges, checkpoints, and human intervention mechanisms. These support branching workflows and recovery for long-running tasks.
- Multi-agent collaboration: Other frameworks emphasize role definitions and task delegation to coordinate multiple agents.
These capabilities address real engineering needs. However, an application that only requires a small subset of them may gain little from a full-featured framework. Instead, the framework can add a substantial learning burden and make application behavior harder to inspect and control.
3. Why Plain Python Can Be a Better Fit
Transparency and Debuggability
In a native implementation, developers explicitly construct each model request payload, each tool’s inputs and outputs, and each piece of context using ordinary strings and dictionaries.
When a model produces an unexpected response or a tool fails, developers can set breakpoints and inspect the actual variables. They do not need to work through multiple layers of framework decorators and closures to understand what happened.
Lightweight, Flexible Execution
Many business agents only need a simple ReAct—Reasoning and Acting—loop:
Receive input → Reason about the task → Call a tool → Observe the result → Respond.
A minimal version of this loop can fit into fewer than 50 lines of Python. That does not make it a complete production system, but it illustrates how small the core execution logic can be.
Direct implementation gives developers control over prompts and execution flow. They can adjust the behavior to fit business requirements without depending on a framework’s predefined lifecycle.
Fewer Dependencies and Lower Maintenance Overhead
Open-source frameworks evolve rapidly, and their APIs can introduce breaking changes. A plain Python implementation can keep its dependencies limited to an HTTP client or a lightweight official SDK.
This reduces exposure to framework-level changes and can make upgrades easier to manage. It also keeps more of the application’s behavior within code the team directly owns.
4. Building a Native AI Agent from Scratch
A robust native agent still needs deliberate component design. The main areas are prompt and context management, tool registration, the execution loop, and production infrastructure.
Prompt and Context Management
Prompts guide the agent’s behavior. A native implementation can use a dedicated prompt manager to assemble system instructions, conversation history, and tool descriptions according to the current task state.
The following function illustrates the basic structure:
def build_prompt(user_query, history, tools):system_prompt = "You are an intelligent assistant that can use tools to solve problems."tool_descriptions = "\n".join([f"- {t['name']}: {t['description']}" for t in tools])# Assemble the context dynamically.messages = [{"role": "system", "content": f"{system_prompt}\nAvailable tools:\n{tool_descriptions}"}]messages.extend(history)messages.append({"role": "user", "content": user_query})return messages
This approach makes the context sent to the model explicit and easy to inspect.
Tool Registration and Execution
An agent needs a way to invoke external tools. Python decorators or a simple dictionary mapping can provide a lightweight registration mechanism. The application can then parse JSON arguments returned by the model and use them to call the corresponding function.
tools_registry = {}def tool(name, description):def decorator(func):tools_registry[name] = {"func": func, "description": description}return funcreturn decorator@tool("get_weather", "Get the weather for a specified city")def get_weather(city: str) -> str:# In a real application, this could call an external weather API.return f"It is sunny in {city} today, with a temperature of 25 degrees."
The example demonstrates registration rather than a complete tool-calling implementation. The registry associates a tool name with its function and description; the execution loop handles the model’s request to invoke it.
The Core Control Loop
The agent’s main loop processes either text responses or tool-call instructions from the model. When the model requests a tool, the application executes the corresponding function and returns the result to the ongoing interaction.
The loop continues until the model provides a final answer. Keeping this control flow explicit makes it easier to understand how model calls, tool execution, and context updates fit together.
Production Infrastructure and Observability
Avoiding an orchestration framework does not eliminate the need for reliable production infrastructure.
Conversation history and context state should be persisted so that a service restart does not erase them. The original deployment approach uses Baidu AI Cloud message queues and object storage to support persistence. For an overseas deployment, service availability and suitability should be verified rather than assumed.
Runtime logs and operational metrics—including model response latency and tool-call success rates—can be connected to logging, monitoring, and alerting services. Baidu AI Cloud’s logging and monitoring services are an example from the original approach, subject to availability in the intended deployment environment.
These infrastructure components support production reliability and observability while leaving the agent’s execution logic in native Python.
5. Choosing the Right Architecture
Frameworks are not inherently a poor choice. They remain useful for rapid prototyping and for standardizing workflows that match their abstractions.
However, direct Python development deserves serious consideration when a team needs tight control over execution and performance, has substantial custom debugging requirements, or only needs a straightforward tool-calling loop. In these situations, avoiding a heavyweight framework can offer better long-term value.
The guiding principle is to choose the simplest, most transparent architecture that meets the business requirements. Learning to build an agent directly with Python and model APIs provides more than independence from a framework: it also helps developers understand the underlying execution model and make better architectural decisions as their applications evolve.
