n4nAI

How many seconds does a 5-step agent loop actually take?

Agent loop latency 5 steps: how sequential LLM calls, tool overhead, and caching shape whether your loop finishes in 15 or 60 seconds.

n4n Team5 min read1,041 words

Audio narration

Coming soon — every post will get a voice note here.

Most teams estimate agent loop latency 5 steps by multiplying a single model call by five. That ignores tool round-trips, context growth, and retry storms—real loops routinely take 20–60 seconds, and sometimes much more when a provider degrades. The thesis here is simple: the wall-clock cost of a five-step agent loop is dominated by sequential dependencies and tail latency, not by raw model inference, and you can predict it with a straightforward time budget.

What a 5-step agent loop actually does

A typical ReAct-style agent runs a fixed number of iterations. Each iteration reads the conversation so far, decides whether to call a tool, waits for the tool, and writes the observation back into context. Five steps means five model completions interleaved with five tool executions, plus the final synthesis if you count that as a step.

def run_loop(messages, tools, steps=5):
    for _ in range(steps):
        resp = client.chat.completions.create(
            model="gpt-4o",
            messages=messages,
            tools=tools,
        )
        msg = resp.choices[0].message
        messages.append(msg)
        if msg.tool_calls:
            result = execute_tool(msg.tool_calls[0])
            messages.append({"role": "tool", "content": result})
    return messages

The code looks trivial. The latency is not.

Breaking down the time budget

LLM inference per step

A cloud LLM API does not return instantly. For a 200-input / 300-output token completion on a GPT-4-class model, time-to-first-token commonly lands at 0.5–1.5 seconds, with full completion at 1–3 seconds under normal load. Smaller models like Claude Haiku or GPT-4o-mini often cut that to sub-second. Multiply by five and you have 5–15 seconds of pure model time.

Tool execution and network

Tools are rarely local. A SQL query, a REST call to Stripe, or a vector search adds 100–800 ms each, sometimes multiple seconds if the downstream service is slow. In a five-step loop with one tool call per step, that is another 1–4 seconds baseline, and it compounds when tools call other tools.

Context growth and prefill blowup

Each step appends the assistant message and the tool result. By step five, your prompt may have grown from 500 to 4,000 tokens. Prefill cost scales with prompt size; a 4k-token context processes slower than a 500-token one on the same model. This silently adds 10–30% to later steps if you do not cache the static prefix.

Serialization and client overhead

Your SDK marshals JSON, the gateway parses it, and TCP/TLS handshakes add milliseconds. Usually negligible (<100 ms per call) but worth noting when you run hundreds of loops per minute.

Measuring real-world agent loop latency 5 steps

Instrument the loop with timestamps. Do not trust vendor marketing; measure your exact prompt and tools.

import time

def timed_loop(messages, tools, steps=5):
    totals = {"llm": 0.0, "tool": 0.0}
    for _ in range(steps):
        t0 = time.perf_counter()
        resp = client.chat.completions.create(model="gpt-4o", messages=messages, tools=tools)
        totals["llm"] += time.perf_counter() - t0
        messages.append(resp.choices[0].message)
        if resp.choices[0].message.tool_calls:
            t1 = time.perf_counter()
            result = execute_tool(resp.choices[0].message.tool_calls[0])
            totals["tool"] += time.perf_counter() - t1
            messages.append({"role": "tool", "content": result})
    return totals

Run this against your production-like workload. In our internal tests with a 4o-class model and a 300 ms internal tool, the agent loop latency 5 steps settled around 12–18 seconds. Swap the model for a slower reasoning variant or add a 1.2 s external API, and you cross 30 seconds easily.

Scenario ranges without caching or fallback:

Configuration Est. LLM time Est. tool time Total loop
Tiny model, LAN tools 3–5 s 1–2 s 8–12 s
Frontier model, 1 external call/step 8–15 s 3–6 s 20–35 s
Frontier model, large context, degraded provider 20–40 s 5–10 s 60+ s

These are ranges, not precise benchmarks, but they reflect the physics of sequential network calls.

Tradeoffs: speed versus quality

You can shrink agent loop latency 5 steps by using a tiny model for planning and a larger one for synthesis. The risk: the small model misplans, causing extra steps or wrong tool calls, which defeats the savings. In one trace we reviewed, swapping the planner to a 8B model saved 4 seconds but produced two malformed tool calls that triggered retries, netting a 3-second loss.

Parallelizing steps is another lever—if step 2 does not depend on step 1’s tool output, fire both completions concurrently:

import asyncio

async def parallel_steps(prompts):
    tasks = [client.chat.completions.create_async(model="gpt-4o-mini", messages=p) for p in prompts]
    return await asyncio.gather(*tasks)

But most agent loops are inherently sequential because each step conditions on the previous observation. Forcing parallelism creates state-management bugs that cost more time in debugging than you save at runtime.

Engineering levers that actually cut latency

Prompt caching

If your system prompt and tool schemas are static across the five steps, cache them. OpenAI-compatible endpoints accept cache-control hints; forward them and avoid re-paying prompt processing each turn.

client.chat.completions.create(
    model="gpt-4o",
    messages=static_prefix + dynamic_suffix,
    extra_headers={"cache-control": "max-age=600"},
)

This trims per-step prompt tokenization and prefill, often shaving 10–20% off LLM time when context is large.

Streaming and incremental parsing

Streaming does not make the full response arrive faster, but it lets you start parsing tool calls or update UI earlier. For loops where the agent emits structured JSON, incremental validation catches errors mid-stream and cancels bad calls.

Fallback and routing

A single provider outage should not add 30 seconds of retries to your agent loop latency 5 steps. An inference gateway that offers automatic fallback when a provider is rate-limited or degraded—and honors client routing directives—keeps tail latency bounded. (n4n.ai exposes one OpenAI-compatible endpoint across 240+ models with exactly that behavior, plus per-token metering so you see the cost of each step.) You still need to set timeouts; never let a step hang longer than your SLA allows.

Timeouts and bounded retries

Implement a per-step deadline of, say, 8 seconds. On timeout, either skip the tool or substitute a cached result. Unbounded exponential backoff inside a loop multiplies latency: five steps each retrying twice at 2s intervals adds 20 seconds of pure waiting.

Trim the step count

The most effective optimization is to ask whether you need five steps. Many tasks collapse to three with a better prompt or a composite tool that returns multiple facts at once. Fewer steps is the only change that linearly reduces latency without quality tradeoffs.

Why “5 steps” is a slippery unit

Step count hides token volume. A “step” that returns a 2-token confirmation is cheaper than one returning a 2,000-token SQL dump. When comparing agent loop latency 5 steps across teams, normalize by total generated tokens and tool round-trip count, not just iterations.

Decisive takeaway

Design every agent loop as if agent loop latency 5 steps will be 30 seconds under normal load and 90 seconds under stress. Cache static context, set hard timeouts per step, use a gateway with fallback to avoid provider tail latency, and question any loop that cannot drop a step. If your product requires sub-10-second agent responses, either reduce steps to two or three, move to streaming UX, or use a local small model for the iterative steps and reserve frontier models for final synthesis. Latency is an architecture problem, not a model parameter.

Tagsagent-loopslatencybenchmarkai-agents

Written by

n4n Team

The team building n4n — a single OpenAI-compatible API in front of 240+ models, with automatic fallback, load balancing and pay-per-token metering.

More from n4n Team →

All agentic workflow performance benchmarks posts →