The Agent class is the high-level planner in the GAME SDK. It coordinates workers and manages the overall agent state.
An agent is responsible for:
- Managing multiple workers
- Creating and tracking tasks
- Making high-level decisions
- Maintaining session state
Here's how to create and use an agent:
from game_sdk.game.agent import Agent
from game_sdk.game.worker_config import WorkerConfig
# Create agent
agent = Agent(
goal="Handle weather reporting",
description="A weather reporting system"
)
# Add worker
worker_config = create_worker_config()
agent.add_worker(worker_config)
# Compile and run
agent.compile()
agent.run()The agent's primary objective:
goal="Provide accurate weather information and recommendations"Detailed description of the agent's purpose and capabilities:
description="""
This agent:
1. Reports weather conditions
2. Provides clothing recommendations
3. Tracks weather patterns
"""Add workers to handle specific tasks:
# Add multiple workers
agent.add_worker(weather_worker_config)
agent.add_worker(recommendation_worker_config)
agent.add_worker(analytics_worker_config)Set specific, measurable goals:
# Good
goal="Provide hourly weather updates for specified cities"
# Bad
goal="Handle weather stuff"Provide comprehensive descriptions:
description="""
Weather reporting agent that:
1. Monitors weather conditions
2. Provides clothing recommendations
3. Tracks temperature trends
4. Alerts on severe weather
"""Organize workers by function:
# Weather monitoring
agent.add_worker(weather_monitor_config)
# Recommendations
agent.add_worker(clothing_advisor_config)
# Analytics
agent.add_worker(trend_analyzer_config)Handle errors at the agent level:
try:
agent.compile()
agent.run()
except Exception as e:
logger.error(f"Agent error: {e}")
# Handle error appropriatelydef create_weather_agent():
"""Create a weather reporting agent."""
# Create agent
agent = Agent(
goal="Provide weather updates and recommendations",
description="""
Weather reporting system that:
1. Monitors current conditions
2. Provides clothing recommendations
3. Tracks weather patterns
"""
)
# Add workers
agent.add_worker(create_weather_worker_config())
agent.add_worker(create_recommendation_worker_config())
return agentdef create_task_manager():
"""Create a task management agent."""
# Create agent
agent = Agent(
goal="Manage and track tasks efficiently",
description="""
Task management system that:
1. Creates and assigns tasks
2. Tracks task progress
3. Generates reports
"""
)
# Add workers
agent.add_worker(create_task_worker_config())
agent.add_worker(create_report_worker_config())
return agentTest your agents thoroughly:
def test_weather_agent():
"""Test weather agent functionality."""
# Create agent
agent = create_weather_agent()
# Test worker addition
assert len(agent.workers) > 0
# Test compilation
agent.compile()
assert agent.is_compiled
# Test execution
result = agent.run()
assert result.status == 'success'# Start new session
session = agent.start_session()
# Resume existing session
agent.resume_session(session_id)# Get agent state
state = agent.get_state()
# Update state
agent.update_state(new_state)# Get worker results
results = agent.get_worker_results()
# Share data between workers
agent.share_data(worker1_id, worker2_id, data)-
Worker Conflicts
- Ensure workers have unique IDs
- Define clear worker responsibilities
- Handle shared resources properly
-
State Management
- Keep state minimal
- Handle state updates atomically
- Document state structure
-
Performance
- Monitor worker execution time
- Optimize resource usage
- Cache frequently used data