FOUNDATIONS · 5 MIN READ

What actually makes an AI agent an agent?

Start with the loop: observe, decide, act, and check what happened.

Download example files Code + setup + checks in a ZIP
Jump to a section 10 sections

EXAMPLE USE CASE

Answer an order question using evidence

A support assistant must look up an order before answering, rather than guess from the question.

Input
Customer asks for A104. A read-only fixture contains A104 = packed.
Expected output
One lookup observation, followed by an answer containing packed.
  • Success check: The answer is produced only after the lookup; the loop stops within three turns.
  • Try a failure: Change A104 to A999. The result must be not_found, with no invented delivery date.

Example scope: A runnable local control-loop lab. The decision is deterministic; the linked SDK example replaces it with a model.

OpenAI Agents SDK exampleLangChain example
SEE HOW THE SYSTEM FITS TOGETHERSELECT A COMPONENT
goaltool callallowedobservationevidenceif incompleteObserveRead the requestDecideChoose the next stepAuthorizeValidate the operationActExecute the toolCheckInspect the observationAnswer or continueStop with evidence
  1. Identify the goal, authenticated user, and evidence already available.

  2. The decision produces an answer or a request for one allowed tool.

  3. Application code checks the tool name, argument schema, identity, and resource scope.

  4. For A104, read the authoritative order status and return a structured observation.

    observationTo 05 · Check
  5. Confirm that the lookup succeeded. Missing data and timeouts require different next steps.

  6. Answer from the returned status, or take another bounded step if evidence is incomplete.

Decide

The decision produces an answer or a request for one allowed tool.

Follow the arrows, then select a component to inspect its responsibility. On mobile, downward arrows connect the next step; bent arrows identify branches and return paths. Select a destination to jump to that component.

A model inside a loop

  • An agent receives a goal, observes the available information, chooses an action, and uses the result to decide what to do next.
  • A language model can help choose the action.
  • Your application supplies the tools, permissions, state, and stopping rules.
  • Imagine a support assistant asked to check an order.
  • It may identify the order number, call a lookup tool, inspect the returned status, and then answer.
  • The action produces new evidence that changes the next step.

Start with a clear boundary

  • Define what the agent can read, what it can change, and when a person must review the result.
  • A tool description tells the model how a capability works.
  • Server-side code decides whether a requested operation is allowed.

Pick a task you can measure

  • Choose one task with observable success: returning the correct order status, identifying a missing field, or producing a grounded summary.
  • Collect representative examples and check the result against the source data.
  • A useful first system has a small tool inventory, a turn limit, explicit error handling, and a result that someone can inspect.

Set up the lab

  • Use a new working directory for this example.
Shell
python3 -m venv .venv
source .venv/bin/activate

Implement the example

Save as loop.py

Python · loop.py
orders = {"A104": "packed"}
observations = []
for turn in range(3):
    # This deterministic decision function makes the loop visible.
    # A model can later replace this decision, within the same boundary.
    if not observations:
        action = {"tool": "lookup_order", "order_id": "A104"}
        assert action["tool"] == "lookup_order"
        observations.append(orders.get(action["order_id"], "not_found"))
        print("TOOL", action, "RESULT", observations[-1])
    else:
        print("ANSWER", observations[-1])
        break
else:
    raise RuntimeError("Turn budget exhausted")

Run and verify

Shell
python loop.py
  • One lookup runs before the answer; the answer is packed.
  • Change A104 to an unknown order: the answer must become not_found, without an invented delivery date.

Troubleshooting and failure tests

  • An answer before the lookup has no tool evidence. Preserve the observation in state.
  • An agent that keeps searching needs a turn budget and an explicit stop condition.

Before production

  • Replace the decision function with one SDK tutorial, retaining the tool allowlist.
  • Authenticate the user before the lookup and record the authoritative result, latency, and stop reason.

Keep exploring

The anatomy of an agentic systemWorkflow, single agent, or multiple agents?Build with the OpenAI Agents SDK