Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Benchmark AI Agent Speed & Resource Use

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Benchmarking an AI agent on your own workload tells you whether it is fast enough for production, how much memory and CPU it consumes, and where the bottlenecks are. This guide walks you through a repeatable method using standard tools, so you can compare agents or versions with confidence. You will need a Linux or macOS machine with Python 3.8+, the `time` command, `psutil` (or `py-spy`), and a representative set of tasks that mirror your real usage.

What You'll Need

  • A computer with a terminal (Linux or macOS recommended; Windows with WSL works too).
  • Python 3.8+ installed, plus `pip`.
  • The AI agent you want to test, installed and working.
  • A set of test prompts or tasks that represent your actual workload (e.g., 50 customer-support queries, 10 code-generation requests).
  • Optional but helpful: `psutil` (Python library for system monitoring), `py-spy` (for profiling), and `hyperfine` (for precise timing).

Step-by-Step

  1. Define your workload. Write down the exact tasks you want to benchmark. Use a realistic mix: short queries, long documents, multi-step tool calls. Save them in a plain text file, one task per line. This becomes your benchmark set.
  2. Set up a clean environment. Close other applications, stop background jobs, and make sure the machine is idle. If you're testing a server-based agent, restart the server to clear caches. For containerized agents, use a fresh container with a known resource limit (e.g., Docker `--memory=2g --cpus=2`).
  3. Warm up the agent. Run a few sample tasks once to load models, connect to APIs, and populate any caches. Discard these results—they don't reflect steady-state performance.
  4. Time each task individually. Write a small script that reads your task file, sends each task to the agent, and records the time from request to final response. Use Python's `time.perf_counter()` for high precision. Save the times to a CSV file with columns: task_id, task_text, elapsed_seconds.
  5. Measure resource usage. While the script runs, sample CPU and memory usage. In Python, use `psutil` to record the agent's process CPU% and RSS (resident set size) every 100ms. Alternatively, run `py-spy record --pid -o profile.svg` to get a flame graph of where time is spent.
  6. Run multiple iterations. Repeat the timing run at least 5 times. This smooths out network jitter, API latency, and random slowdowns. For each task, record the median and the 95th percentile latency.
  7. Calculate aggregate metrics. From your data compute: average latency per task, throughput (tasks per minute), peak memory (max RSS), and average CPU usage. Also note the total time for the whole workload.
  8. Compare against a baseline. If you're benchmarking a new version or a different agent, run the same procedure on the baseline and compare side-by-side. Keep the hardware and environment identical—change only the agent.
  9. Document everything. Record the agent version, model names, temperature settings, hardware specs, and the date. This makes your results reproducible and defensible.

Common Mistakes

  • Not warming up. Skipping the warm-up gives misleadingly slow first-run times due to model loading and cache misses.
  • Inconsistent workloads. Changing the task list between runs makes results incomparable. Always use the same file.
  • Ignoring background noise. Other processes, auto-updates, or network activity can skew timings. Run on an idle machine, and repeat runs to spot outliers.
  • Measuring only wall-clock time. Wall time includes network and API latency, which may not reflect the agent's own speed. If you need to isolate the agent's compute, measure from the moment it receives the request to the moment it returns the response.
  • Forgetting to reset state. If your agent has memory or context, a previous run can influence the next. Use a fresh session or clear context between runs.

Tips

  • Use `hyperfine` for command-line agents—it gives you mean, median, and standard deviation with minimal effort.
  • Record the number of tokens processed (input + output) if your agent exposes it. This lets you compute tokens-per-second, which is more meaningful than raw latency.
  • For long-running workloads, monitor memory over time to detect leaks. A steady increase in RSS across tasks is a red flag.
  • If you're benchmarking a multi-agent system, measure each component separately (e.g., the planner, the tool executor) to find the bottleneck.
  • Automate the whole process with a script so you can re-run it after every code change.

Troubleshooting

The agent crashes during the benchmark. Check for out-of-memory errors. Reduce the concurrency or increase the container memory limit. If it's a Python agent, run it with `--preload` to catch import errors early.

Timings are wildly inconsistent. This usually indicates network variability or background processes. Run more iterations, and consider using a local model instead of a cloud API if the goal is to measure the agent's logic, not the network.

Resource usage shows 0% CPU. The agent might be waiting on I/O (e.g., API calls). Use `py-spy` to see if the process is idle or blocked. If it's a multi-process agent, monitor all child processes, not just the parent.

Memory usage seems too high. Some agents cache embeddings or conversation history. Check if there's a memory limit setting. Also, ensure you're measuring the correct process—use `psutil.Process(pid).memory_info().rss` for each child.

The benchmark takes too long. Reduce the number of tasks, but keep at least 10 per category. You can also limit the response length in the agent's config to speed up runs.

Frequently Asked Questions

Q: How many tasks should I include in my benchmark set?
A: Enough to be representative but not so many that it takes hours. 20-50 tasks covering your typical use cases is a good start.

Q: Should I benchmark with a local model or an API?
A: It depends on your goal. If you're optimizing the agent's code, use a local model to remove network noise. If you're evaluating end-user experience, include the API latency.

Q: What is the most important metric to report?
A: For most production workloads, the 95th percentile latency (p95) is more important than the average, because it shows worst-case user experience. Also report throughput if you handle many requests concurrently.

Q: Can I benchmark a conversational agent that maintains state?
A: Yes, but you must define a session. Decide whether each task is a fresh conversation or a continuation. For continuation, you need to provide the conversation history in the task file.

Q: How do I measure memory usage of a subprocess agent?
A: Use `psutil.Process(pid).memory_info().rss` for each child process, and sum them. Or use `py-spy record` which includes memory stats.

Q: What if my agent uses external tools that are slow?
A: That's part of the workload. But if you want to isolate the agent's planning speed, mock the tool calls to return instantly. Then you can compare the agent's logic without external dependencies.