Can You Build an AI Agent in Pure Python Without a Framework?

    This article examines whether a production-ready AI agent can be implemented in native Python without an orchestration framework. It covers where complexity actually comes from, how vendors differ on abstraction, what frameworks encapsulate, a minimal agent loop, the engineering work you must own yourself, and a practical decision matrix.

    1. Where Agent Complexity Really Comes From

    In engineering practice, “whether to adopt an orchestration framework” is almost a required question for agent projects. The debate is rarely about whether something can run, but about who maintains it after it does. A usable agent essentially does three things: assemble context into a string, call a model API for structured output, and decide from that output whether to loop again. All three can be expressed clearly with native Python.

    The real complexity is not in the main path, but in constraints around it: retries, rate-limit backoff, context trimming, tool-argument validation, state persistence, cost accounting, and evaluation. A framework’s value is packaging these cross-cutting capabilities; its cost is that once they sit behind an abstraction, you may not understand them when something breaks.

    2. A Spectrum of Attitudes Toward Abstraction

    Providers differ clearly on how much abstraction they want. You can line them up on a spectrum:

    • Most cautious: some model vendors, in public agent-building guides, advise starting with direct model API calls. Frameworks can hide prompts and raw responses, make debugging harder, and push teams into unnecessary complexity when a simple design is enough. They also note that wrong assumptions about framework internals are a common source of production issues.
    • Middle path: some vendors ship a lightweight, in-house agent toolkit that is code-first and wraps only a minimal set of capabilities.
    • Open interoperability: some vendors prefer to work with multiple orchestration options and leave the choice to developers.
    • Most framework-friendly: some vendors argue that for most teams a mature framework is usually safer, because teams do not have to rebuild capabilities that already exist.

    These positions sit in different commercial and technical contexts; none is simply right or wrong. A more reliable engineering test is whether your team can read and change the abstraction layer.

    3. What Frameworks Actually Encapsulate

    To judge alternatives, you first need to see what would be replaced. Mainstream orchestration typically offers:

    1. A unified model interface. It hides differences in request formats, streaming protocols, and tool-call structures across vendors, which lowers migration cost.
    2. Chain-style orchestration. Multi-step calls are wired into a directed flow with input/output mapping.
    3. An autonomous decision loop. The model chooses the next action until it produces a final answer.
    4. A tool layer. External systems are wrapped as model-callable functions, with argument parsing and result injection.
    5. State and memory. Session history, summarization, and short- and long-term memory are managed.

    On top of that, graph-style orchestration adds explicit state graphs, conditional branches, checkpoint recovery, and human-in-the-loop nodes, which help with complex branching and resuming long tasks. Multi-agent setups define roles, goals, and context for division of labor and delegation.

    These abstractions map to real pain. You only need one question: does your flow have branching and recovery that you cannot express with sequential code plus a loop?

    4. A Minimal Kernel: An Agent Main Loop in About Sixty Lines

    A native core can stay very small:

    1. def run_agent(goal, tools, model, max_steps=12):
    2. messages = [{"role": "user", "content": goal}]
    3. for step in range(max_steps):
    4. resp = model.chat(messages=messages, tools=tools) # thin unified client
    5. messages.append(resp.message)
    6. if not resp.tool_calls:
    7. return resp.text
    8. for call in resp.tool_calls:
    9. result = dispatch(call.name, call.args) # validate args + execute
    10. messages.append(tool_result(call.id, result))
    11. raise RuntimeError("step budget exhausted")

    There is no magic: string assembly, API calls, loop control. That readability is the source of maintainability—when production fails, you can go straight to the prompt, the arguments, or the return value.

    5. Engineering You Must Provide If You Skip a Framework

    Dropping a framework is not dropping quality; it means implementing packed capabilities explicitly:

    • Thin client: wrap only auth, timeouts, stream parsing, and error classification. Do not add another abstraction on top.
    • Retries and rate limits: separate retryable errors (rate limits, timeouts, 5xx) from non-retryable ones (invalid arguments, content blocks), with exponential backoff and jitter.
    • Context governance: set a token budget, use a sliding window plus summarization, and truncate tool results while stating that truncation happened.
    • Tool contracts: declare parameters with a structured schema, validate types and enums before execution, and feed readable errors back to the model instead of raising a stack.
    • State persistence: write steps, tool calls, and intermediate artifacts to external storage so you can resume and support human review.
    • Observability: log prompt version, token usage, latency, and a summary of tool results; connect logs and monitoring alerts.
    • Evaluation loop: keep a fixed test set and regress on prompt and model changes so you do not iterate on “it feels better.”
    • Cost control: cap steps and spend per task so the loop cannot run away.

    On the cloud, these map to familiar building blocks: object storage for sessions and artifacts, a message queue for long tasks, a container platform for runtime, and logging/monitoring for traces and alerts.

    6. Decision Matrix

    Prefer a native implementation when the flow is linear, tools are few, the team is strong at engineering, you need deep customization of prompts and output formats, and latency and cost matter.

    Prefer a framework when you have complex branches and long-task recovery, need multi-agent collaboration, have a large team that needs shared conventions, or want speed in a prototype phase.

    A practical path is staged: prove feasibility with a native loop, distill stable cross-cutting pieces into a thin internal library, and only then adopt graph-style orchestration if branching and recovery truly outgrow sequential code—not by taking on every abstraction on day one.

    7. Conclusion

    Yes, you can hand-roll an agent in pure Python. Feasibility is not the same as low cost. Frameworks and native code are not opposites; they differ in who owns the abstraction layer—the community or you. The criterion is not popularity, but whether the team can read and fix it the night it fails.