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

    A production-grade AI agent is a loop of task intake, model calls, tool execution, and next-step decisions. This article explains when a general-purpose agent framework is enough, when custom Python is the better engineering choice, and how to structure a minimal core of loop, tools, memory, and observability.

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

    An agent is not mysterious: accept a task, call a model, run tools, decide the next step, and repeat until the work is done. Those four steps can be implemented in a few hundred lines of native Python. Running a demo and running in production are different problems. Engineering, robustness, and maintainability sit between them. This piece looks at what generic agent frameworks actually give you, what they cannot, and how to design a core architecture if you build your own.

    Decide by Scenario, Not by Camp

    Choosing a framework versus writing your own is an engineering decision, not a matter of belief.

    A general-purpose framework fits when:

    • You are in prototype mode and only need to test whether an agent can do the task.
    • The path is a typical RAG Q&A, document retrieval, or relatively standard workflow.
    • The team does not yet know agent internals and needs the framework to hide details.

    Custom work fits when:

    • Latency and throughput are hard requirements and extra abstraction cost is unacceptable.
    • Business logic is highly specific, so framework abstractions become constraints and glue code exceeds the framework itself.
    • You need fine-grained control of state transitions, error recovery, and audit logs.
    • Agent behavior must be deeply customized: decision policy, multi-agent topology, or precise cost control.

    Large organizations often rebuild not because other wheels are bad, but because those wheels do not fit the road they need to run. With a framework you live inside its capability boundary; with custom code you can go beyond it.

    What Generic Frameworks Do Not Give You

    Their value is standardization. The cost is de-personalization. Three production needs often sit outside that comfort zone.

    1. Efficiency
    To support many models, tools, and memory mechanisms, frameworks add layers of wrapping. Each tool call pays serialization and event-loop overhead. At low concurrency you may not notice; at scale the cost multiplies. Optimizing then can cost more than rewriting the path yourself.

    2. Personalization
    Extension points are the ones the framework authors planned. If you need a policy such as “on tool failure, rewrite the prompt by failure type and retry three times,” you either get lucky or you hack around the public path. Enough hacks and the framework is only drag.

    3. Robustness
    Production agents must handle format drift in model output, tool timeouts, network jitter, inconsistent state, and runaway loops. In many frameworks these paths are configurable but opaque. When something breaks, it is hard to tell whether the model, the framework, or your config is at fault.

    Minimal Core Architecture for a Custom Agent

    If you write it yourself, the core is clear. An agent has five parts:

    1. ┌─────────────────────────────────────┐
    2. │ Agent Loop │
    3. │ ┌─────────┐ ┌──────────────┐ │
    4. │ │ State │ ←→ │ Model client │ │
    5. │ └─────────┘ └──────────────┘ │
    6. │ ↑ ↓ │
    7. │ ┌─────────┐ ┌──────────────┐ │
    8. │ │ Memory │ │ Tool registry │ │
    9. │ └─────────┘ └──────────────┘ │
    10. │ ↑ ↓ │
    11. │ ┌─────────────────────────────┐ │
    12. │ │ Executor / error handling │ │
    13. │ └─────────────────────────────┘ │
    14. └─────────────────────────────────────┘

    1. Agent loop

    The skeleton is “model inference → tool execution → write results back.” Pseudocode:

    1. class Agent:
    2. def __init__(self, llm, tools, memory, max_steps=20):
    3. self.llm = llm
    4. self.tools = {t.name: t for t in tools}
    5. self.memory = memory
    6. self.max_steps = max_steps
    7. def run(self, task: str):
    8. self.memory.add("user", task)
    9. for step in range(self.max_steps):
    10. response = self.llm.chat(self.memory.messages(),
    11. tools=list(self.tools.values()))
    12. if not response.tool_calls:
    13. return response.content # task complete
    14. for call in response.tool_calls:
    15. result = self._safe_execute(call)
    16. self.memory.add_tool_result(call.id, result)
    17. raise LoopLimitExceeded(self.max_steps) # prevent runaway

    max_steps is easy to skip and must not be skipped. Agents looping forever are a common production incident; you need a hard cutoff.

    2. Tools

    Each tool is a function plus a structured description (name, purpose, parameter schema). The model picks tools via a function-calling protocol; you implement reliable dispatch and execution:

    1. class Tool:
    2. def __init__(self, name, description, func, schema):
    3. self.name, self.description = name, description
    4. self.func, self.schema = func, schema

    A production executor also needs timeouts, parameter validation, and structured errors returned to the model so it can self-correct, instead of crashing.

    3. State and memory

    This is where custom code pays off. You can define a state machine for the business: which steps can roll back, what must persist, how to summarize when the context window fills. Generic frameworks offer generic policies; those are often not optimal for a specific product.

    4. Observability

    Log every loop step: input messages, model output, tool arguments, latency, token usage. Structured logs in a logging service plus alerts make agent behavior auditable, replayable, and tunable. It is not complex, but it decides whether the agent can actually ship.

    A Practical Hybrid

    For most teams the realistic path is not all custom or all framework:

    • Write the differentiated core yourself: the main loop, decision policy, state, and domain tools—the source of product advantage.
    • Reuse the ecosystem for standard layers: model access, embeddings, vector search, object storage, logging and monitoring. Do not rebuild those.

    The test is simple: does this layer contain your business know-how? If yes, write it. If not, use a common library or managed service.

    Summary

    A pure-Python agent is not only feasible; it is how many high-requirement production systems are built. It is a trade: higher development cost for full control of efficiency, customization, and robustness. If you are still on a standard path, use a framework to get running. When the framework’s boundary becomes yours, that is the time to start from Python.