0
0
Agentic_aiml~15 mins

Latency monitoring per step in Agentic Ai - ML Experiment: Train & Evaluate

Choose your learning style8 modes available
Experiment - Latency monitoring per step
Problem:You have an AI agent that performs multiple steps sequentially. Each step takes some time to complete. Currently, you do not know how long each step takes, which makes it hard to optimize or debug the agent's performance.
Current Metrics:No latency data per step is collected. Total execution time is known but step-wise timing is unknown.
Issue:Without step-level latency monitoring, it is difficult to identify slow steps or bottlenecks in the agent's workflow.
Your Task
Add latency monitoring to measure and report the time taken by each step of the AI agent's process.
Do not change the logic or output of the agent's steps.
Use simple timing methods available in Python.
Ensure the latency data is printed or logged clearly after the agent finishes.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
Agentic_ai
import time

def step1():
    time.sleep(0.5)  # Simulate work
    return "Step 1 done"

def step2():
    time.sleep(0.7)  # Simulate work
    return "Step 2 done"

def step3():
    time.sleep(0.3)  # Simulate work
    return "Step 3 done"

def run_agent_with_latency_monitoring():
    latencies = {}

    start = time.time()
    result1 = step1()
    latencies['step1'] = time.time() - start

    start = time.time()
    result2 = step2()
    latencies['step2'] = time.time() - start

    start = time.time()
    result3 = step3()
    latencies['step3'] = time.time() - start

    print(result1)
    print(result2)
    print(result3)

    print("Latency per step (seconds):")
    for step, latency in latencies.items():
        print(f"{step}: {latency:.3f}s")

if __name__ == "__main__":
    run_agent_with_latency_monitoring()
Added timing code around each step using time.time() to measure start and end times.
Stored each step's latency in a dictionary.
Printed latency summary after all steps complete.
Results Interpretation

Before: No timing data per step, only total time known.

After: Each step's latency is measured and printed, allowing identification of slow steps.

Measuring latency per step helps find bottlenecks and optimize AI agent workflows by showing exactly where time is spent.
Bonus Experiment
Modify the code to log latency data to a CSV file instead of printing it.
💡 Hint
Use Python's csv module to write step names and latencies to a file for later analysis.