Full example
How to run an agent with the xpander AI SDK
Understanding the While Loop and Agent Progress
The while loop is a crucial component in the agent’s execution model, enabling it to:
-
Progress Through the Agent Graph: The agent moves through different nodes in the graph system, executing tools and making decisions based on the conversation context.
-
Maintain Conversation State: Each iteration maintains the conversation history in the
memory
list, allowing the agent to reference previous interactions and tool results. -
Handle Multi-Step Tasks: Complex tasks often require multiple steps - the loop enables the agent to:
- Make function calls
- Process results
- Plan next actions
- Move to different graph nodes
-
Control Flow Management: The loop continues until the agent determines it has completed its task, signaled by including “##FINAL ANSWER##” in its response.
Loop Components Explained:
number_of_calls
: Tracks iteration count for monitoring and debuggingllm_response
: Gets the LLM’s next action/response based on conversation historytool_calls
: Handles function calling when the agent needs to use toolsmemory
: Maintains the full conversation context including tool results
The agent uses this loop structure to navigate through the Agent Graph System, ensuring systematic progression through complex workflows while maintaining context and state.
For more examples with different LLM providers (OpenAI, Anthropic, Ollama, etc.), check out our Agents Hub Examples Repository.