Qora API — AI API Gateway for Developers

AI API Gateway for Developers

One clear API workflow for your apps, scripts and automations.

AI Agents 101: Orchestrating Multi-Step Tasks with Tool Use

Cover image for an AI agents 101 guide showing the three ingredients of an agentic workflow: tool use, loops, and planning.

An AI agent is a model wrapped in a loop. Instead of answering once, it plans a step, calls a tool, reads the result, and decides what to do next — repeating until the task is done or a budget stops it. Tool use gives the model the ability to act; orchestration is what keeps that action safe and observable.

That definition is deliberately unglamorous, because most of what separates a working agent from a demo is engineering discipline rather than model capability. This guide covers the loop itself, how to plan multi-step work, how to bound runaway execution, and the observability you need before you let an agent touch anything real.

What actually makes something an “agent”

Three properties separate an agent from a chat completion:

  • Tools. The model can request actions — search, query a database, call an API, write a file — not just produce text.
  • A loop. Tool results feed back into the model, which produces another step. The number of model calls is decided at runtime, not by your code.
  • State. Something persists across steps: the conversation, a scratchpad, a task list, or all three.

Remove any one and you have something simpler. No tools and it is a chatbot. No loop and it is a single-shot function call. No state and it cannot do anything requiring more than one step. The interesting engineering is entirely in how you manage the loop and the state.

The agent loop, step by step

Almost every agent framework, however it is branded, implements the same cycle:

PhaseWhat happensWhat you must control
ObserveAssemble the goal, history, and latest tool results into contextContext size — trim aggressively or the loop gets expensive fast
PlanThe model reasons about the next step, or revises the whole planWhether you re-plan every step or once up front
ActThe model emits a tool call with argumentsArgument validation before execution
ExecuteYour code runs the tool and captures the resultTimeouts, retries, idempotency, permissions
EvaluateDecide: done, retry, or continueThe stopping condition — the most commonly missing piece

Note that four of the five phases are your responsibility, not the model’s. An agent that “goes off the rails” almost always means one of those four controls was missing.

Planning: decompose before you act

There are two broad planning styles, and the right choice depends on how predictable the task is.

Plan-then-execute asks the model to produce a full step list up front, then works through it. This is cheaper — one planning call instead of reasoning on every step — and far easier to audit, because you can show a user the plan before anything runs. It is the right default for structured, repeatable workflows like “gather these five data points and produce a report.”

Interleaved (or reactive) planning lets the model decide the next step from the latest observation. This handles genuinely open-ended tasks where step two depends on what step one returned. The cost is that the agent can wander, and the trace is harder to explain after the fact.

Most production agents are hybrids: plan a coarse outline up front, then allow bounded re-planning when an observation invalidates an assumption. The key discipline is to make re-planning an explicit, logged event rather than an invisible drift.

Tool use: giving the agent hands

A tool is a function with a name, a description, and a typed parameter schema. The model reads that description and decides when to call it — which means your tool descriptions are prompt engineering, and vague descriptions produce vague behavior. The underlying request/response mechanics are covered in our guide to AI function calling and tool use; what matters for orchestration is the shape of your tool surface.

  • Few, well-scoped tools beat many overlapping ones. If two tools could plausibly handle a request, the model will pick inconsistently.
  • Return structured, compact results. A tool that dumps 40KB of raw JSON burns context and degrades every subsequent decision. Return the fields the agent needs.
  • Make failures explicit and legible. A tool that returns "no results" teaches the agent to try a different query; a tool that throws an opaque exception teaches it to retry forever.
  • Separate read tools from write tools. Reads are safe to retry freely. Writes need confirmation, idempotency keys, and usually a human in the path.

Typed steps keep the loop honest

The single highest-leverage reliability technique in agent engineering is forcing every model output into a schema. When each step must conform to a typed object — {"thought": ..., "tool": ..., "args": {...}, "done": false} — three good things happen: malformed steps get rejected before execution, your orchestrator can branch on a real field instead of parsing prose, and every step becomes a row you can log and replay.

That is a structured-output problem, not a prompting trick. Our guide to structured outputs and JSON mode covers how to enforce a schema at the decoding layer so the agent physically cannot emit an unparseable step.

A minimal agent loop you can read

Strip away the frameworks and an agent is a bounded while-loop. This version is deliberately small, but it contains every control that matters — a step budget, validated arguments, error feedback, and an explicit termination flag:

MAX_STEPS = 12          # hard ceiling on loop iterations
MAX_SECONDS = 60        # wall-clock budget for the whole task

def run_agent(goal, tools, client):
    messages = [
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": goal},
    ]
    started = time.time()

    for step in range(MAX_STEPS):
        if time.time() - started > MAX_SECONDS:
            return {"status": "timeout", "steps": step}

        # 1) Ask the model for exactly one typed step.
        step_out = client.chat.completions.create(
            model=ROUTER["agent"],
            messages=messages,
            response_format={"type": "json_object"},   # enforced schema
        )
        action = json.loads(step_out.choices[0].message.content)

        # 2) Terminate explicitly, never by guessing at prose.
        if action.get("done"):
            return {"status": "ok", "answer": action["answer"], "steps": step}

        # 3) Validate before executing anything.
        name = action["tool"]
        if name not in tools:
            messages.append({"role": "user",
                             "content": f"Unknown tool '{name}'. Choose from {list(tools)}."})
            continue
        try:
            result = tools[name](**action["args"])
        except Exception as exc:
            result = {"error": str(exc)}      # feed failure back, don't crash

        # 4) Return a compact observation, then loop.
        messages.append({"role": "assistant", "content": json.dumps(action)})
        messages.append({"role": "user", "content": f"Observation: {json.dumps(result)[:2000]}"})

    return {"status": "max_steps", "steps": MAX_STEPS}

Four lines in that function do more for reliability than any model upgrade: MAX_STEPS, MAX_SECONDS, the unknown-tool branch, and the exception-to-observation conversion. Without them, a single ambiguous tool result can turn into an infinite loop that bills you by the minute.

Stopping conditions and runaway control

Agents fail expensively in ways chatbots cannot, because each iteration can cost money and cause side effects. Layer these limits; do not rely on one:

GuardWhat it preventsTypical trigger
Step ceilingInfinite tool loops12–25 steps
Wall-clock timeoutSlow tools stalling a task30–120 seconds
Token budgetContext growth blowing up costCumulative token cap per task
Repeated-action detectorThe same call with the same args, foreverIdentical call seen twice
Write confirmationDestructive side effectsHuman approval or dry-run mode
Tool-level rate limitHammering an external APIPer-tool quota per task

The repeated-action detector is the one people forget. Models get stuck in a groove — calling the same search with identical arguments and getting the same empty result — and the step ceiling is the only thing that saves you. Detect the repeat and inject a message telling the agent that approach already failed.

Observability: the trace is the product

You cannot debug an agent from its final answer. You need the trace: every step’s thought, tool name, arguments, raw result, latency, token count, and model used. Treat the trace as a first-class artifact and three things get dramatically easier — root-causing failures, building an evaluation set from real runs, and proving to a reviewer what the agent actually did.

Two practices pay for themselves immediately. First, log every step as structured JSON, not as a formatted string, so you can query it. Second, replay traces against a new prompt or model before you ship a change — a frozen set of real traces is the only honest regression test for an agent.

Routing: different steps want different models

Agent loops are where model routing pays off most, because a single task might involve a dozen model calls of wildly different difficulty. Planning a research task is hard reasoning; extracting a date from a tool result is trivial. Sending both to a frontier model is the most common way agent costs get out of hand.

Route by role: a frontier model for the planning step, a mid-tier model for the main reasoning loop, and a small/fast model for classification, extraction, and summarization of tool output. Critically, validate tool-use reliability before you route agent steps to a cheaper model — a model that is fine in chat can be unreliable at structured function calls, and one malformed step can derail an entire run. Our guide to choosing and routing AI models covers the tiering and fallback design.

Keeping that routing flexible is an architectural concern, not a detail. If every provider needs its own client, auth, and request shape, then changing the model behind one step becomes a refactor and your agent ossifies around whichever vendor you wired up first. A unified, OpenAI-compatible endpoint reduces that to a string — which is exactly what an AI API relay provides. If you want to try the routing pattern without maintaining four integrations, qoraapi.com exposes many models behind one OpenAI-compatible base URL.

Common agent failure modes

  • No stopping condition. The loop ends when it feels done. It never feels done.
  • Context bloat. Every tool result appended verbatim until the prompt is enormous and the model loses the plot. Summarize or truncate observations.
  • Overlapping tools. Ambiguous tool surfaces make the model choose erratically — consolidate before you add.
  • Silent tool errors. Swallowing an exception makes the agent believe the action succeeded, and it builds on a false premise.
  • Unvalidated arguments. Passing model-generated arguments straight into a shell, query, or write call is an injection risk. Validate and whitelist.
  • Irreversible writes without approval. Give the agent read access first, add writes behind confirmation, and only automate what you have watched succeed repeatedly.
  • No trace. Without step-level logs you are debugging by intuition, which does not scale past one example.

Frequently asked questions

What is an AI agent in simple terms?

An AI agent is a language model placed inside a loop with access to tools. It plans a step, calls a tool such as a search or database query, reads the result, and repeats until the task is complete or a budget stops it. The model supplies the reasoning; the loop and the guardrails are code you write.

What is the difference between tool use and an agent?

Tool use is one capability — the model’s ability to request a function call with arguments. An agent is a system built on top of that capability: a loop that feeds tool results back to the model, plus state, a stopping condition, and observability. You can have tool use without an agent, but you cannot have a useful agent without tool use.

How do you stop an AI agent from looping forever?

Impose layered limits rather than one: a maximum step count, a wall-clock timeout, a cumulative token budget, and a detector that flags the same tool call with identical arguments appearing twice. Also require an explicit done flag in a typed output schema, so termination is a declared decision rather than something you infer from prose.

Do AI agents need a frontier model?

Not for every step. Planning and hard reasoning usually benefit from a frontier model, but most loop iterations are extraction, classification, or formatting that a mid-tier or small model handles at a fraction of the cost. Route by step role, and validate tool-calling reliability on the cheaper model before you depend on it.

Are AI agents safe to run in production?

Yes, with the same discipline you would apply to any automated system. Start read-only, add write actions behind explicit confirmation, validate and whitelist tool arguments, cap steps and spend, and log a full trace of every step. Agents become risky when they have unvalidated write access and no audit trail — not because of the model itself.

Conclusion

An AI agent is a model in a loop, and the loop is ordinary software: typed steps, validated arguments, bounded iterations, explicit termination, and a trace you can replay. Get those right and the model’s job becomes much easier, because it only has to reason one step at a time inside a structure that keeps it honest. Get them wrong and no amount of model capability will save the run.

Start with one narrow, read-only task, log every step, and grow the tool surface only after the trace looks clean. When you are ready to make the model layer swappable, begin with our AI API gateway guide and the OpenAI-compatible API explainer.

Related reading

Build AI features with one clear API

Qora API gives you a single, focused gateway to connect your apps, scripts and automations to AI. Start with one request.

qoraapi.com · AI API gateway for developers

Comments

4 responses to “AI Agents 101: Orchestrating Multi-Step Tasks with Tool Use”

  1. […] Continue building your AI API stack: AI Structured Outputs Explained: JSON Mode, Schema Enforcement, Reliable Parsing · AI Prompt Engineering for Reliable API Responses · AI Agents 101: Orchestrating Multi-Step Tasks with Tool Use. […]

  2. […] and your tools stay in the low double digits with distinct names — the territory of our guide to AI agents and tool use. Reliability work — retries, validation, guardrails — pays off long before […]

  3. […] AI Agents 101: Orchestrating Multi-Step Tasks with Tool Use […]

  4. […] hardening the rest of the loop, read our guide to building reliable AI agents and the piece on AI agents and function calling — memory is what makes those patterns persist across sessions. And because a […]

Leave a Reply

Your email address will not be published. Required fields are marked *