Pure Python AI Agents Without a Framework: Is It Feasible?

    This article examines whether AI agents can be built effectively in pure Python without an agent development framework. It breaks LLM applications down to their core components—string handling, API calls, and loop control—and explains why native Python is sufficient for many agent workloads. It also reviews the practical costs of framework abstractions, including harder debugging, limited flexibility, and over-engineering. A compact tool-calling agent example using Baidu AI Cloud's Qianfan platform illustrates how prompt construction, JSON parsing, tool execution, and result feedback can remain transparent and controllable. Finally, it outlines when raw Python is the better choice and when a framework still makes sense.

    As LLM applications have grown, AI agents have become a major focus, and development frameworks and automation solutions continue to emerge. They promise highly encapsulated interfaces that let developers quickly build agents with autonomous decision-making, multi-step planning, and external tool invocation. Yet in real production environments, a notable pattern is emerging: some teams use mainstream frameworks for a year and then choose to remove them entirely. Their feedback is that the team is happier and more productive after removal.

    This raises a core question: without these AI agent development frameworks, is it really feasible to build agents in pure Python? The answer is yes. But before discussing how to do it, we need to understand both the value and the limits of frameworks objectively.

    1. The Essence of LLM Applications: Why Native Python Is Enough

    What is an LLM application, once the architectural jargon is stripped away? There are really only three core elements: string handling, API calls, and loop control.

    1. String handling: LLM inputs and outputs are text. We need to assemble user intent, history, and tool results into prompts in a specific format, then parse the text returned by the model.
    2. API calls: The only standard way to interact with an LLM is an HTTP request.
    3. Loop control: An agent’s “intelligence” is expressed as a think-act-observe loop until the task is complete or a termination condition is reached.

    As a high-level language, Python’s built-in string handling, rich HTTP libraries such as requests, and basic flow-control statements already fit these three core elements perfectly. From a technical foundation perspective, native Python is fully capable of building complex agents.

    2. The Limits of Frameworks: The Backlash of Abstraction

    Mainstream LLM development frameworks do solve real engineering problems, such as inconsistent API formats across vendors and the inability of a single call to complete complex tasks. They provide abstractions such as unified model interfaces, chained calls, tool integration, and memory management. However, these extra abstraction layers can become a double-edged sword in some scenarios.

    1. Debugging nightmares and black-box effects

    When a framework chains multiple steps together and hides internal details, troubleshooting becomes extremely difficult once an agent behaves unexpectedly. Stack traces are often buried deep in multiple nested callbacks, and developers cannot easily see the final prompt passed to the LLM. A well-known LLM vendor has specifically warned that incorrect assumptions about framework internals are a common source of customer technical issues.

    2. Flexibility constraints

    For the sake of generality, frameworks often assume a standard process. But when you need special branching logic or a customized memory-decay strategy, the framework’s structure becomes a constraint. Developers have to read large amounts of source code, look for unstable extension points, and even use hacks to force changes to the framework’s default behavior.

    3. Encouraging over-engineering

    Many patterns can be implemented in just a few lines of code, but a framework’s rich features can tempt developers to add unnecessary complexity when a simple solution would suffice. For example, forcing a simple question-answering system into an orchestration framework with advanced features such as state graphs, conditional edges, checkpoints, and human intervention not only increases system overhead but also significantly raises maintenance costs.

    3. A Practical Guide to Building AI Agents in Pure Python

    Developers should start by using the LLM API directly. For example, using Baidu AI Cloud’s Qianfan platform, we can implement an agent with tool-calling capability in a very lightweight way.

    The core logic is a while loop that repeatedly decides, based on the LLM output, whether to call an external tool or finish the task.

    1. import json
    2. import requests
    3. # Example configuration for the Baidu AI Cloud Qianfan platform API
    4. API_URL = "https://qianfan.baidubce.com/v2/chat/completions"
    5. API_KEY = "your_api_key"
    6. # Define available tools
    7. def get_weather(location):
    8. # Simulate an external API call
    9. return f"{location} is sunny today with a temperature of 25°C."
    10. available_tools = {
    11. "get_weather": get_weather
    12. }
    13. def run_agent(user_query):
    14. messages = [
    15. {"role": "system", "content": "You are an assistant. If you need weather information, output a JSON command to call a tool. Format example: {\"action\": \"get_weather\", \"args\": {\"location\": \"Beijing\"}}"},
    16. {"role": "user", "content": user_query}
    17. ]
    18. max_iterations = 5
    19. for _ in range(max_iterations):
    20. # 1. Call the LLM API (string handling and API call)
    21. response = requests.post(
    22. API_URL,
    23. headers={"Authorization": f"Bearer {API_KEY}"},
    24. json={"messages": messages, "model": "ernie-4.0-8k"}
    25. )
    26. llm_output = response.json()['choices'][0]['message']['content']
    27. # 2. Observe and parse the output
    28. try:
    29. # Try to parse whether the output contains a tool-call command
    30. if "{" in llm_output and "}" in llm_output:
    31. # Simplified parsing logic
    32. json_str = llm_output[llm_output.find("{"):llm_output.rfind("}")+1]
    33. action_obj = json.loads(json_str)
    34. if "action" in action_obj:
    35. tool_name = action_obj["action"]
    36. tool_args = action_obj["args"]
    37. # 3. Act: execute the tool
    38. if tool_name in available_tools:
    39. result = available_tools[tool_name](**tool_args)
    40. # Feed the tool result back to the LLM
    41. messages.append({"role": "assistant", "content": llm_output})
    42. messages.append({"role": "user", "content": f"Tool execution result: {result}"})
    43. continue
    44. except json.JSONDecodeError:
    45. pass
    46. # If there is no tool call, the LLM has provided the final answer
    47. return llm_output
    48. return "Maximum number of iterations reached. Task terminated."
    49. # Test execution
    50. print(run_agent("What is the weather like in Beijing today?"))

    The code above is fewer than 50 lines and clearly shows the full picture of an agent: constructing prompts, making requests, parsing JSON, executing functions, and feeding results back to the model. There is no hidden black box; all logic remains under the developer’s control. During debugging, simply printing the messages list is enough to fully trace the agent’s reasoning path.

    4. When to Use a Framework and When to Build It Yourself

    Emphasizing native development does not mean rejecting the value of frameworks entirely. The two are not opposites; they fit different scenarios.

    Scenarios suitable for native development:

    • The core path is simple and does not require complex multi-branch orchestration.
    • Latency and performance requirements are extremely high, and extra serialization/deserialization overhead is undesirable.
    • The team wants strong control over low-level logic and needs highly customized prompt engineering and state management.
    • The project is in a rapid prototype-validation stage.

    Scenarios suitable for introducing a framework:

    • You need to connect to LLMs from many different providers and urgently need a unified adapter layer to avoid vendor lock-in.
    • Business processes are extremely complex and involve advanced workflow orchestration needs such as checkpoint recovery for long-running tasks and human review.
    • You are building a multi-agent collaboration system and need standardized role definitions and task delegation mechanisms to reduce communication costs within the team.

    Conclusion

    Building AI agents in pure Python without an agent development framework is not only feasible but often the better choice. Frameworks are tools for solving complex common problems; they should not become shackles that limit developers’ thinking. When we return to the essence of LLM applications and use the most basic code to assemble logic, we often find that the so-called “agent” is actually made of clear, controllable engineering code. Start by calling the API directly, master the underlying principles, and introduce a suitable framework only when it is truly needed. That is the mature developer’s path.