Goal setting & monitoring
The goal setting pattern gives your AI system the ability to set measurable objectives, track progress, and adapt its strategy when things aren't going well. It's the difference between "do stuff" and "achieve this specific outcome."
Goal monitoring loop
The agent uses the LLM to analyze why progress stalled and suggest a new approach. For example, if a web scraping goal fails because of rate limits, it might switch to a cached data source or reduce request frequency.
class Goal:
def __init__(self, name, description, target_value, current_value=0):
self.name = name
self.target_value = target_value
self.current_value = current_value
self.status = "in_progress"
def get_progress_percentage(self):
return min(100, (self.current_value / self.target_value) * 100)
class GoalTracker:
def __init__(self):
self.goals = {}
self.llm = get_llm()
def add_goal(self, name, description, target):
self.goals[name] = Goal(name, description, target)
def update_progress(self, name, value):
goal = self.goals[name]
goal.current_value = value
if goal.current_value >= goal.target_value:
goal.status = "completed"
def generate_strategy(self, name):
goal = self.goals[name]
prompt = f"""
Goal: {goal.name}
Progress: {goal.current_value}/{goal.target_value}
({goal.get_progress_percentage():.0f}%)
Generate an adapted strategy to reach this goal.
"""
return self.llm.generate(prompt).contentGoal and GoalTracker classes with progress monitoring.
Quiz: Quiz
Loading practice…
Matching exercise: Match goal setting concepts
Loading practice…
Flashcards: Flashcards
Loading practice…
Goal monitoring gives your agents purpose and direction. Instead of just responding to requests, they can now track progress toward objectives and adapt when things are not working. Next, we will learn how to handle the inevitable failures with exception handling and recovery patterns.