xpander.ai’s agent system uses a sophisticated state management approach that integrates various components. This guide explains the fundamental concepts of agent state and how to get started.
from xpander_sdk import XpanderClient, LLMProviderfrom openai import OpenAIimport os# Initialize clientsxpander_client = XpanderClient(api_key=os.environ.get("XPANDER_API_KEY"))openai_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))# Get your agentagent = xpander_client.agents.get(agent_id=os.environ.get("XPANDER_AGENT_ID"))# Add a taskagent.add_task(input="Tell me about machine learning")# Run the agent until the task is completewhile not agent.is_finished(): # Get LLM response response = openai_client.chat.completions.create( model="gpt-4.1", messages=agent.messages, tool_choice=agent.tool_choice, temperature=0.0 ) # Add LLM response to memory agent.add_messages(response.model_dump()) # Extract and execute tool calls tool_calls = agent.extract_tool_calls( llm_response=response.model_dump(), ) if tool_calls: agent.run_tools(tool_calls=tool_calls)# Get the final resultresult = agent.retrieve_execution_result()print(f"Result: {result.result}")
You can update an agent’s configuration using the update() and sync() methods:
Copy
Ask AI
# Update agent instructions# The instructions property is a dictionary with 'role', 'general', and 'goal' keysagent.instructions = { "role": "You are an AI assistant specialized in machine learning", "general": "Provide clear and concise explanations about machine learning concepts", "goal": "Help users understand machine learning terms and applications"}# Apply the changesagent.update() # Updates the agent configurationagent.sync() # Deploys the agent with all changes# Verify changesprint(f"Agent instructions: {agent.instructions}")