Bird
Raised Fist0
Agentic AIml~20 mins

Monitoring agent behavior in production in Agentic AI - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - Monitoring agent behavior in production
Problem:You have deployed an AI agent that interacts with users in real time. The agent sometimes behaves unexpectedly or makes mistakes, but you do not have a system to track and analyze its behavior after deployment.
Current Metrics:No current metrics available because there is no monitoring system in place.
Issue:Without monitoring, it is hard to detect when the agent makes errors or behaves poorly, which can harm user experience and trust.
Your Task
Create a monitoring system that logs the agent's actions and decisions in production, and provides simple metrics such as error rate and average response time.
You cannot change the agent's core decision-making model.
The monitoring system must not slow down the agent's response time significantly.
Hint 1
Hint 2
Hint 3
Solution
Agentic AI
import time
import threading

class AgentMonitor:
    def __init__(self):
        self.logs = []
        self.lock = threading.Lock()
        self.error_count = 0
        self.total_requests = 0

    def log_action(self, action, success):
        timestamp = time.time()
        with self.lock:
            self.logs.append({'time': timestamp, 'action': action, 'success': success})
            self.total_requests += 1
            if not success:
                self.error_count += 1

    def get_metrics(self):
        with self.lock:
            error_rate = (self.error_count / self.total_requests) * 100 if self.total_requests > 0 else 0
            return {'total_requests': self.total_requests, 'error_rate_percent': error_rate}

# Example agent function
monitor = AgentMonitor()

def agent_action(input_data):
    # Simulate agent processing
    time.sleep(0.01)  # simulate response time
    # Simulate success or failure randomly
    import random
    success = random.random() > 0.1  # 90% success rate
    monitor.log_action(action=input_data, success=success)
    return 'response' if success else 'error'

# Simulate production usage
for i in range(100):
    agent_action(f'user_input_{i}')

metrics = monitor.get_metrics()
print(metrics)
Added AgentMonitor class to log agent actions and success status.
Implemented thread-safe logging to avoid slowing down the agent.
Tracked total requests and error counts to compute error rate.
Simulated agent actions with random success to demonstrate monitoring.
Results Interpretation

Before: No monitoring, no metrics available.

After: Total requests logged: 100, Error rate around 10% detected.

Monitoring agent behavior in production helps detect errors and measure performance, enabling improvements without changing the agent's core logic.
Bonus Experiment
Extend the monitoring system to track average response time and alert if error rate exceeds 15%.
💡 Hint
Record start and end times for each action to calculate response time. Use simple conditional checks to trigger alerts.

Practice

(1/5)
1. What is the main purpose of monitoring agent behavior in production?
easy
A. To understand how agents perform in real situations
B. To write new code for agents
C. To delete old agent data
D. To stop agents from running

Solution

  1. Step 1: Understand monitoring goal

    Monitoring is used to observe and understand agent actions during real use.
  2. Step 2: Identify correct purpose

    Among options, only understanding agent performance matches monitoring's goal.
  3. Final Answer:

    To understand how agents perform in real situations -> Option A
  4. Quick Check:

    Monitoring purpose = Understand behavior [OK]
Hint: Monitoring means watching agents work live [OK]
Common Mistakes:
  • Confusing monitoring with coding
  • Thinking monitoring deletes data
  • Assuming monitoring stops agents
2. Which command is correct to check agent error logs in production?
easy
A. agent show errors
B. agent logs --errors
C. agent error-logs
D. agent --check errors

Solution

  1. Step 1: Review command syntax

    The correct command uses 'agent logs --errors' to fetch error logs.
  2. Step 2: Compare options

    Only agent logs --errors matches typical command style with correct flags and order.
  3. Final Answer:

    agent logs --errors -> Option B
  4. Quick Check:

    Correct flag usage = agent logs --errors [OK]
Hint: Look for commands with correct flags and order [OK]
Common Mistakes:
  • Using wrong flag order
  • Missing double dashes for flags
  • Using spaces instead of dashes
3. Given this command output:
agent status --id 1234
Output:
{"id":1234,"status":"active","errors":0,"speed":5}
What does the speed value represent?
medium
A. Agent's uptime in hours
B. Number of errors encountered
C. Agent's ID number
D. Agent's current processing speed

Solution

  1. Step 1: Analyze output fields

    The output shows keys: id, status, errors, speed. Speed likely means processing speed.
  2. Step 2: Match speed meaning

    Speed is not errors or ID or uptime, so it represents processing speed.
  3. Final Answer:

    Agent's current processing speed -> Option D
  4. Quick Check:

    Speed field = processing speed [OK]
Hint: Speed usually means how fast agent works [OK]
Common Mistakes:
  • Confusing speed with errors count
  • Thinking speed is agent ID
  • Assuming speed means uptime
4. You run agent monitor --id 5678 --interval 10 but get an error: Unknown option: --interval. What is the likely fix?
medium
A. Use --refresh instead of --interval
B. Remove the --id option
C. Change 5678 to a string like '5678'
D. Run the command as root user

Solution

  1. Step 1: Identify error cause

    Error says --interval is unknown, so flag is invalid.
  2. Step 2: Find correct flag

    Documentation shows --refresh is the correct flag for interval timing.
  3. Final Answer:

    Use --refresh instead of --interval -> Option A
  4. Quick Check:

    Correct flag for timing = --refresh [OK]
Hint: Check error message for unknown flags, replace with correct ones [OK]
Common Mistakes:
  • Removing required options
  • Changing data types unnecessarily
  • Ignoring error message details
5. You want to monitor agent errors and speed every 5 minutes and save results to a file named agent_report.json. Which command correctly does this?
hard
A. agent monitor --errors --speed --interval 300 > agent_report.json
B. agent monitor --errors --speed --interval 5 > agent_report.json
C. agent monitor --errors --speed --refresh 300 > agent_report.json
D. agent monitor --errors --speed --refresh 5 > agent_report.json

Solution

  1. Step 1: Identify correct timing flag

    From previous knowledge, --refresh is correct flag for interval in seconds.
  2. Step 2: Convert 5 minutes to seconds

    5 minutes = 5 * 60 = 300 seconds, so use 300 as value.
  3. Step 3: Check output redirection

    Using > agent_report.json saves output to file as required.
  4. Final Answer:

    agent monitor --errors --speed --refresh 300 > agent_report.json -> Option C
  5. Quick Check:

    Use --refresh 300 and redirect output [OK]
Hint: Use --refresh with seconds, redirect output with > [OK]
Common Mistakes:
  • Using --interval instead of --refresh
  • Using 5 instead of 300 seconds
  • Forgetting to redirect output