Can You Build a Python AI Agent Without a Framework?

    This article examines whether building an AI agent in pure Python, without an orchestration framework, is practical. It breaks down the four core capabilities an agent needs, explains the debugging costs introduced by abstraction layers, and outlines a minimal four-layer architecture with sample code. It also covers the engineering problems you must solve yourself, such as context budgeting, tool contract design, error recovery, and observability, and when a framework is still the better choice.

    1. The Real Question: Do You Need an Agent Framework?

    When building LLM-based agent applications, a common debate is whether you must adopt a mature orchestration framework. Proponents emphasize development speed and ecosystem completeness. Skeptics argue that an agent is fundamentally a software engineering problem, not a challenge that requires a dedicated runtime.

    Breaking the question apart shows the skeptics are closer to engineering reality. A working agent has only four core requirements: assembling context, calling the model API, parsing structured output, and running a control loop that drives multi-turn behavior. Python’s native language features and standard library are enough to cover all four.

    The cost structure matters even more. Adopting a framework adds a layer of indirection. You no longer control prompts directly; you control them indirectly through the framework’s templates and hooks. You no longer read raw responses; you extract fields through the framework’s parsers. When behavior matches expectations, this indirection saves effort. When behavior deviates, the debugging path gets significantly longer, because you must first determine whether the problem lies in the model, the prompt, or the framework’s wrapping logic.

    2. A Deliberate Stance in Official Documentation: The Cost of Abstraction

    One model vendor’s engineering practice documentation offers a notably restrained recommendation. Such frameworks lower the barrier to entry by wrapping common low-level tasks, but they also stack on extra abstraction layers that obscure the actual prompts and responses, making debugging harder. The documentation adds that frameworks can tempt developers into unnecessary complexity when a simpler approach is already sufficient.

    It further recommends starting from direct model API calls, since many common patterns require only a small amount of code. It also warns that incorrect assumptions about a framework’s internal implementation are a frequent source of production incidents.

    The value of this statement is not that it rejects frameworks. It establishes an order: first understand the behavioral boundaries of the bare API, then decide whether to introduce abstraction. Without that understanding, a framework turns from an accelerator into a black box.

    3. Counter-Evidence from Production: More Efficient After Removal

    Teams in the industry have publicly reviewed similar experiences. After using a certain class of orchestration framework for roughly a year, they chose to remove it entirely and return to a self-built lightweight calling layer. The feedback was that both team efficiency and maintainability improved.

    The reasons usually concentrate on three points. First, breaking changes from version upgrades require continuous follow-up. Second, the framework’s abstraction does not match business semantics, forcing the team to write adapter code. Third, troubleshooting requires jumping back and forth between framework source code and business code.

    This kind of review does not amount to a universal conclusion that frameworks are useless. What it shows is that when business logic is specific enough and the team is familiar enough with the API, the marginal benefit of a self-built control layer exceeds the convenience a framework provides.

    4. A Minimal Viable Architecture: Four Layers

    A practical hand-written agent can be abstracted into four layers:

    1. Access layer: wraps model API calls and handles timeouts, retries, rate limiting, and error code mapping in one place.
    2. Context layer: assembles and trims system instructions, message history, tool results, and external retrieval snippets.
    3. Decision layer: parses the model’s structured call intent and decides whether to keep calling tools or end the turn.
    4. Execution and observability layer: runs tools, writes logs to disk, and records the input, output, and latency of every step.

    The illustrative code below shows the point: the loop itself is the control flow, with no hidden scheduler.

    1. def run_agent(task, tools, max_steps=12):
    2. messages = [{"role": "system", "content": SYSTEM_PROMPT},
    3. {"role": "user", "content": task}]
    4. for step in range(max_steps):
    5. resp = call_model(messages, tools=tools) # Access layer
    6. messages.append(resp.message)
    7. if not resp.tool_calls: # Decision layer: converged
    8. return resp.text
    9. for call in resp.tool_calls: # Execution layer
    10. result = tools[call.name](**call.args)
    11. messages.append(tool_message(call.id, result))
    12. return "Maximum steps reached, escalating to a human"

    5. Four Problems You Must Solve Yourself

    Once the framework is gone, the following engineering problems must be designed explicitly, and this is exactly where self-building delivers value.

    Context budget and trimming. The context window is a finite resource, so priorities must be clear: system instructions and the current task cannot be trimmed; tool definitions load on demand; message history uses a sliding window or summary compression. It is advisable to add instrumentation that estimates token usage, avoiding sudden overflow during long tasks.

    Tool contract design. A tool’s name, description, and parameter schema directly determine the model’s calling accuracy. Descriptions should state “when to use” rather than only “what it is”; parameters should be as flat and enumerated as possible; high-risk operations should require a confirmation parameter.

    Error recovery strategy. Distinguish retryable errors (rate limiting, timeouts) from non-retryable ones (parameter validation failures). For the latter, feed the validation information back to the model so it can correct itself in the next round, instead of throwing an exception that terminates the flow.

    Observability. For every call, record the request identifier, prompt version, response summary, tool latency, and final status. Without these fields, production problems are nearly impossible to locate. Logging, metrics, and alerting can rely on general-purpose logging services and monitoring and alerting capabilities, with no need to bind to a specific vendor’s API.

    6. When a Framework Is Still the Right Choice

    Self-building is not the only answer. If the goal is to validate an idea quickly, if the team lacks API experience, or if you need many ready-to-use task templates, a framework’s wrapping can noticeably shorten the delivery cycle.

    The rational criterion is whether the abstraction matches the way you understand the system. If you can clearly state what every layer of the framework does, it is a tool. If you cannot, it is a risk.

    7. Conclusion

    Building an agent in pure Python without an orchestration framework is not only feasible; in scenarios with clear logic and high controllability requirements, it is often the better solution. The real barrier is not the amount of code but whether you understand the underlying mechanisms: context management, tool contracts, loop termination, and observability. Getting the bare API working first and introducing abstraction only as needed is the more reliable engineering path. When building such applications on platforms like Baidu AI Cloud, you can likewise use only the model API along with general-purpose storage, logging, and monitoring capabilities, keeping the control layer fully in your own hands.