Latency
Latency measures how long the agent takes to complete tasks. High latency affects user experience and operational cost. Identifying the bottlenecks is the first step to improvement.
6 min•By Priygop Team•Updated 2026
Latency Profiling
Latency Profiling
# Latency profiling for agent steps
import time
from collections import defaultdict
class LatencyProfiler:
def __init__(self):
self.timings = defaultdict(list) # action -> [duration_ms]
def time_action(self, action: str, fn, **kwargs):
"""Execute fn and record its latency."""
start = time.perf_counter()
try:
result = fn(**kwargs)
finally:
elapsed_ms = (time.perf_counter() - start) * 1000
self.timings[action].append(elapsed_ms)
return result
def report(self) -> dict:
"""Generate latency report for all actions."""
report = {}
for action, times in self.timings.items():
times_sorted = sorted(times)
n = len(times_sorted)
report[action] = {
"calls": n,
"avg_ms": round(sum(times) / n, 1),
"min_ms": round(min(times), 1),
"max_ms": round(max(times), 1),
"p50_ms": round(times_sorted[n // 2], 1),
"p95_ms": round(times_sorted[int(n * 0.95)], 1),
"p99_ms": round(times_sorted[int(n * 0.99)], 1),
}
return report
# Simulate profiling several actions
profiler = LatencyProfiler()
def mock_web_search(query):
time.sleep(0.05) # 50ms simulated
return {"results": ["r1"]}
def mock_summarise(text):
time.sleep(0.02) # 20ms simulated
return {"summary": "..."}
for _ in range(10):
profiler.time_action("web_search", mock_web_search, query="test")
profiler.time_action("summarise", mock_summarise, text="content")
report = profiler.report()
for action, stats in report.items():
print(f" {action:15} avg={stats['avg_ms']}ms p95={stats['p95_ms']}ms calls={stats['calls']}")