0
0
Agentic_aiml~5 mins

Latency monitoring per step in Agentic Ai

Choose your learning style8 modes available
Introduction

Latency monitoring per step helps you see how long each part of a process takes. This helps find slow parts and fix them.

You want to improve the speed of a multi-step task.
You need to find which step causes delays in your system.
You want to track performance changes after updates.
You want to report detailed timing to your team or users.
Syntax
Agentic_ai
monitor_latency(step_name: str) -> float

Call this function at the start or end of each step.

It returns the time taken in seconds for that step.

Examples
Measure how long 'step1' takes by recording start and end times.
Agentic_ai
start_time = monitor_latency('step1')
# do step 1 work
end_time = monitor_latency('step1')
latency = end_time - start_time
Get latency for a database query step and print it.
Agentic_ai
latency = monitor_latency('database_query')
print(f"Database query took {latency:.2f} seconds")
Sample Program

This program measures how long two steps take by recording start and end times using the monitor_latency function.

Agentic_ai
import time

def monitor_latency(step_name: str, start_times={}):
    now = time.time()
    if step_name in start_times:
        latency = now - start_times.pop(step_name)
        return latency
    else:
        start_times[step_name] = now
        return 0.0

# Start step 1
monitor_latency('step1')
time.sleep(1.2)  # simulate work
# End step 1
latency1 = monitor_latency('step1')

# Start step 2
monitor_latency('step2')
time.sleep(0.8)  # simulate work
# End step 2
latency2 = monitor_latency('step2')

print(f"Step 1 latency: {latency1:.2f} seconds")
print(f"Step 2 latency: {latency2:.2f} seconds")
OutputSuccess
Important Notes

Always call monitor_latency first to start timing, then call it again to get the latency.

Use unique step names to avoid confusion.

Latency is measured in seconds with decimals for precision.

Summary

Latency monitoring per step helps find slow parts in a process.

Call the function at start and end of each step to measure time.

Use the results to improve system speed and user experience.