Latency monitoring per step helps you see how long each part of a process takes. This helps find slow parts and fix them.
Latency monitoring per step in Agentic AI
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
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
Agentic AI
start_time = monitor_latency('step1') # do step 1 work end_time = monitor_latency('step1') latency = end_time - start_time
Agentic AI
latency = monitor_latency('database_query') print(f"Database query took {latency:.2f} seconds")
Sample Model
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")
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.
Practice
1. What is the main purpose of latency monitoring per step in a process?
easy
Solution
Step 1: Understand latency monitoring
Latency monitoring measures how long each step in a process takes.Step 2: Identify the goal of monitoring
The goal is to find slow steps to improve overall speed and user experience.Final Answer:
To find slow parts in the process and improve speed -> Option CQuick Check:
Latency monitoring = Find slow parts [OK]
Hint: Latency monitoring finds slow steps to speed up process [OK]
Common Mistakes:
- Thinking it adds more steps
- Confusing with user count
- Assuming it adds features
2. Which code snippet correctly measures latency for a step using start and end time calls?
easy
Solution
Step 1: Identify correct order of time calls
Start time must be recorded before the step, end time after the step.Step 2: Calculate latency as difference
Latency is end_time minus start_time to get duration.Final Answer:
start_time = get_time()\n// step code\nend_time = get_time()\nlatency = end_time - start_time -> Option AQuick Check:
Latency = end - start [OK]
Hint: Latency = end time minus start time [OK]
Common Mistakes:
- Subtracting start from end incorrectly
- Assigning latency before step runs
- Swapping start and end times
3. Given this code snippet measuring latency per step, what is the output of
print(latencies)?
latencies = []
for step in range(3):
start = get_time()
do_work(step)
end = get_time()
latencies.append(end - start)
print(latencies)
Assume do_work(step) takes 1, 2, and 3 seconds respectively.medium
Solution
Step 1: Understand loop and timing
Each loop iteration measures time before and afterdo_work(step).Step 2: Calculate latencies per step
Sincedo_worktakes 1, 2, and 3 seconds, latencies list will be [1, 2, 3].Final Answer:
[1, 2, 3] -> Option AQuick Check:
Latencies match step durations [OK]
Hint: Latency list matches step durations in order [OK]
Common Mistakes:
- Reversing latency order
- Assuming zero latency
- Ignoring step durations
4. You wrote this code to measure latency per step but get wrong results:
start = get_time() do_step1() end = get_time() latency1 = end - start start = get_time() do_step2() latency2 = end - startWhat is the error causing wrong latency2?
medium
Solution
Step 1: Check timing for latency2
For latency2, end time is not updated after do_step2, so it uses old end value.Step 2: Identify missing end time update
Must callend = get_time()after do_step2 before calculating latency2.Final Answer:
end time is not updated before calculating latency2 -> Option BQuick Check:
Update end time after step [OK]
Hint: Always update end time after each step [OK]
Common Mistakes:
- Forgetting to update end time
- Calculating latency before step ends
- Mixing start and end times
5. You want to monitor latency per step in a multi-step process and alert if any step exceeds 2 seconds. Which approach correctly implements this?
hard
Solution
Step 1: Understand requirement for per-step latency
We must measure each step's latency individually to detect slow steps.Step 2: Implement alert condition per step
Calculate latency per step and trigger alert if latency exceeds 2 seconds.Final Answer:
Measure start and end time per step, calculate latency, and trigger alert if latency > 2 -> Option DQuick Check:
Alert on per-step latency > 2 seconds [OK]
Hint: Alert when any step latency exceeds threshold [OK]
Common Mistakes:
- Measuring only total time
- Checking only first step
- Ignoring timing data
