Agent loop profiling measures the time and resources spent rebuilding prompts on each iteration of an agentic loop, distinguishing that work from the inference (model generation) step. You feed it a trace or log of an agent run, and it returns the fraction of wall-clock time and token usage attributable to prompt reconstruction versus the model call itself.
Formula
Prompt rebuild fraction = (Total loop time – Inference time) / Total loop time × 100%. The variables are: t_total (seconds for one full agent loop iteration, including tool calls, parsing, and prompt assembly), t_inf (seconds spent inside the model inference call itself), tokens_prompt (number of tokens in the assembled prompt sent to the model each iteration), and tokens_gen (tokens generated by the model per iteration). The first output is the percentage of loop time that is not inference—this is the overhead you want to minimize.
Worked Example
Using the defaults: t_total = 120 s, t_inf = 80 s, tokens_prompt = 4500, tokens_gen = 350. Rebuild fraction = (120 – 80) / 120 × 100% = 33.3%. So one-third of the loop time is spent on prompt reconstruction and other non-inference work. The prompt tokens rebuilt each iteration are 4500, and generated tokens are 350—a 12.9:1 ratio of input to output tokens.
Why Prompt Rebuilds Matter
In agentic systems, each loop iteration typically re-serializes the full conversation history, tool schemas, system instructions, and any retrieved context into a fresh prompt. This is distinct from inference, which is the forward pass of the model over that prompt. Profiling that separates the two reveals a common bottleneck: even when the model itself is fast, the prompt assembly can dominate latency if it involves large context windows, repeated JSON serialization, or expensive template rendering. A 2024 survey of LLM agent frameworks noted that prompt construction is often overlooked in performance analyses, leading developers to optimize the wrong layer.
How to Profile an Agent Loop
- Instrument the loop: wrap the model call in a timer, and record the wall-clock time before and after prompt assembly.
- Log token counts: use the tokenizer or the model’s usage statistics to capture prompt and completion token counts per iteration.
- Separate phases: break the loop into prompt build, tool call, parsing, and inference, and record each duration.
- Aggregate across iterations: compute the mean, median, and p95 for each phase to see the distribution.
- Compare with the calculator: input the totals to get the rebuild fraction and token ratio.
Most tracing tools (OpenTelemetry, LangSmith, or custom decorators) can capture these timings with minimal overhead. The key is to ensure the timer for inference starts exactly when the model API is called and ends when the response is fully received, excluding network retries if you want pure compute time.
Choosing the Right Metrics
- Wall-clock fraction (our first output) is the most direct indicator of where time goes. If it’s above 30%, prompt rebuilds are likely worth optimizing.
- Token ratio (prompt tokens vs. generated tokens) tells you if the prompt is bloated. A ratio above 10:1 suggests you’re sending a lot of context that may not be needed.
- Rebuild frequency: count how many times the same prompt is rebuilt unchanged. Caching the prompt (or parts of it) can eliminate redundant work.
- Serialization cost: measure the time spent converting objects to JSON or text. This is often a hidden cost in complex tool schemas.
Use these metrics together: a low wall-clock fraction but high token ratio means inference is the bottleneck, so you should focus on model efficiency (e.g., smaller model, fewer generated tokens). A high fraction with a low token ratio points to inefficient prompt assembly code.
Common Mistakes and Misconceptions
A frequent mistake is attributing all non-inference time to “prompt rebuild” when it actually includes tool execution, network latency, or parsing. The calculator assumes you’ve separated those out. Another misconception is that prompt rebuilds are negligible because they’re CPU-bound—but with large contexts (50k+ tokens), serialization can take hundreds of milliseconds, which adds up over many iterations. Also, many developers measure only the model’s latency from the API response, missing the client-side assembly time entirely. Finally, beware of double-counting: if you cache the prompt and only append new messages, the rebuild time per iteration drops dramatically, but your profiling must reflect that optimization.
How to Reduce Prompt Rebuild Overhead
- Cache static parts: system prompts and tool schemas rarely change; store them as pre-tokenized blobs and concatenate with the dynamic part.
- Use a faster serialization: replace JSON with a compact format like MessagePack if your framework supports it, or use a streaming serializer.
- Trim conversation history: summarize old messages or drop them beyond a window, reducing the token count and rebuild time.
- Parallelize assembly: if you have multiple tool results to embed, build them concurrently.
- Reuse the tokenized prefix: many tokenizers allow incremental encoding; keep the prefix tokens and encode only the new messages.
These optimizations directly lower the rebuild fraction, freeing up time for more iterations or reducing end-to-end latency.
Comparison with Inference Profiling
Inference profiling focuses on the model’s forward pass: time per token, time to first token, and throughput. Prompt rebuild profiling is complementary—it looks at the client-side work. A full agent profile should include both, because optimizing one without the other often yields diminishing returns. For example, if inference takes 800ms and rebuild takes 200ms, cutting rebuild to 50ms saves 150ms (18% total), while cutting inference by 10% saves 80ms. The right choice depends on your numbers—use the calculator to see which is larger.
Limits of This Metric
The rebuild fraction is a high-level indicator, not a complete profiler. It doesn’t tell you why the rebuild is slow—whether it’s tokenization, serialization, or I/O from retrieving context. For that, you need a profiler like cProfile or py-spy to drill into the assembly code. Also, the fraction is sensitive to network variability if the inference time includes network round-trips; for pure compute, measure the model’s time from the first byte sent to the last byte received. Finally, the metric assumes a single model call per iteration; if your loop makes multiple calls (e.g., tool selection + generation), you need to sum their times separately.
Frequently Asked Questions
How do I know if my prompt rebuild is the bottleneck?
If the rebuild fraction (from the calculator) is above 30% and the token ratio is high, prompt rebuild is likely a significant contributor. You can confirm by temporarily caching the prompt and seeing if the loop time drops substantially.
What’s a good target for rebuild fraction?
There’s no universal number, but for latency-sensitive agents, keeping it under 20% is reasonable. For batch processing, even 50% might be acceptable if inference dominates overall cost. The key is to compare it against your inference time and end-user expectations.
Does this work for any agent framework?
Yes, the concept applies to any loop that assembles a prompt and calls a model, whether you use LangChain, AutoGen, or a custom implementation. You just need to instrument the two phases: prompt assembly and model call.
Can I measure this without modifying my code?
If you have a trace that logs timestamps for each step, you can compute the times offline. Otherwise, you’ll need to add minimal instrumentation—a few timers around the prompt build and inference call.
Is prompt rebuild the same as “preprocessing”?
Not exactly. Preprocessing can include data cleaning or retrieval that happens before the loop. Prompt rebuild specifically refers to constructing the model input from the current state (history, tools, context) on each iteration. Retrieval is a separate cost that should be measured independently.
Related
Formula used
in t_total | Total loop time | s | 120 | 0 in t_inf | Inference time | s | 80 | 0 in tokens_prompt | Prompt tokens per iteration | tokens | 4500 | 0 in tokens_gen | Generated tokens per iteration | tokens | 350 | 0 out rebuild_frac | Prompt rebuild fraction of time | % | (t_total - t_inf) / t_total * 100 | 1 out rebuild_tokens | Prompt tokens rebuilt per iteration | tokens | tokens_prompt | 0 out gen_tokens | Generated tokens per iteration | tokens | tokens_gen | 0
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Word Select Last Line in Paragraph?
- Google Assistant IFTTT Recipes: 7 Useful Automations
- OLED TVs in Dark Rooms: Why They Look Better and When They Don’t
- Philips DST7060/20 7000 Series Iron: Specs & Fit
- Coal Fueled Jet Aircraft: Could It Work?675 L
- Remote Desktop on Raspberry Pi: How to Enable It