AI in production

Building AI agents in Python: six design rules

An agent that completes a task on its own in a demo is impressive. An agent that does it flawlessly a thousand times a week takes design decisions you make upfront.

AI in production3 min readPython People editorial team

Start with the task, not the agent

Many agent projects start by picking a framework. Start instead by asking whether you need an agent at all. An agent makes sense when the steps can't be fixed in advance and the model has to decide what to do next.

If you can draw the process as a fixed workflow, build a workflow, with a language model only where language is needed. It's cheaper, faster and far easier to test.

Six design rules

  1. Narrow, typed tools

    Give the agent a small set of tools with strictly defined inputs, for example using Pydantic schemas. Each tool does one thing and validates its own input.

  2. Least privilege

    An agent gets the permissions of the user it acts for, never more. Read access is the default; write access is a deliberate exception.

  3. A human where it matters

    Decide per action whether it can be undone. Irreversible or costly actions, like paying, messaging customers or deleting data, wait for approval.

  4. Cap steps and cost

    Set a maximum on steps, runtime and budget per task. An agent that gets stuck stops and asks for help.

  5. Trace everything

    Log every step: which tool, with what input, what came back and why. Without that trace you can't explain errors, so you can't fix them.

  6. Evaluate at task level

    Measure whether the task was completed correctly, not whether the text sounds convincing. Test with realistic scenarios, edge cases and malicious input.

Prompt injection is a design problem

An agent that reads emails, documents or web pages may find instructions in them that didn't come from your user. A better prompt alone won't fix that.

Treat everything from outside as data, not as instructions. Limit what the agent can do with that data, and route sensitive actions past a person or a fixed check. A successful injection then becomes a nuisance, not a breach.

Why it pays off

Gartner expects over 40% of agentic AI projects to be cancelled by the end of 2027, due to escalating costs, unclear business value and inadequate risk controls. Source: SD Times on Gartner, 30 June 2025 (opens in a new tab)

The rules above address exactly those points. They don't make an agent smarter, but they make it predictable, affordable and explainable. That's what keeps an agent in production.

In Python: typed tools and a step budget

Two of the six rules show up directly in code: tools with a strict schema and a hard limit on the number of steps. A simplified example:

from pydantic import BaseModel, Field

class LookupOrder(BaseModel):
    """Tool: look up an order. Read only."""
    order_id: str = Field(pattern=r"^ORD-[0-9]{6}$")

MAX_STEPS = 8

def run_agent(task, model, tools):
    for step in range(MAX_STEPS):
        action = model.next_action(task, tools)
        if action.done:
            return action.result
        tools.execute(action)  # every step is logged
    raise RuntimeError("Step budget exhausted: task goes to a person")

In practice you would use a framework such as Pydantic AI, LangGraph or the OpenAI Agents SDK, and connect systems through the Python SDK of the Model Context Protocol. Langfuse and OpenTelemetry are common for tracing. The principle stays the same: the framework may change, the rules don't.

Need a Python developer?

Call us or send your vacancy. Within 48 hours you hear which developers in our network fit and what it costs.