What you will achieve in this quickstart guide:

  1. Design an AI Agent in the Workbench, including: The AI Agent state machine, tools (API function calling), and system instructions.
  2. Use the visual tester to test the behavior and tool calling.
  3. Chat to the AI Agent from an external source (fully hosted Chainlit).
  4. Apply the design and configuration to an AI Agent in code.

Using the AI Agent Workbench

1

Log in to the xpander.ai platform.

Go to https://app.xpander.ai and sign in with your credentials.

2

Open the AI Agent Workbench.

In the left navigation menu, go to AI Agents, then click the New AI Agent button.

3

Use the Planner to automatically design your AI Agent via a single prompt

For this quickstart, use the following prompt: “Build an AI Agent that is able to get the top stories and their item details from Hacker News”

This action will automatically: design the AI Agent state machine, add the relevant tools to the AI Agent, and write the AI Agent system level instructions.
It could take up to a minute for the Planner agent to complete the AI Agent design and configuration.

4

Your result should be similar to:

5

Manually add another operation (tool) to the AI Agent: Hover over the bottom operation in the graph -> click the + button. Then, go to xpander functions -> send email -> click Done.

6

Save your AI Agent configuration by clicking the Deploy button.

7

Use the Agent Tester (left pane) to prompt your AI Agent and view its behavior live

Insert a test prompt such as: Get the top 3 news items from hackernews and send them over to <your email>

You will see the decision making process in the graph indicated by the moving Purple dot, as well as the request/response process and payloads in the left pane.

If email sending doesn’t work despite having added the Email sending operation to the AI Agent’s tools, go to the AI Agent’s settings, and in the Instructions settings, add the following:
if asked to send an email, use the send_email operation

You successfully used the AI Agent Workbench to build and test your agent!
Continue the tutorial to trigger and use your AI Agent from external sources.

Chat to you AI Agent from a fully-managed Chainlit instance.

1

Open Chainlit

Click the Chat trigger node at the top of the graph, and click the URL for your hosted Chainlit instance.

2

You can continue prompting your AI Agent from this external source.

You successfully chatted to your AI Agent from an external source.

xpander-sdk in your AI Agent code

The magic of the xpander.ai Agent Platform starts here

The state machine, tools, instructions and other configurations will now be enforced by the xpander SDK in your own AI Agent code.

Set up Python virtual environment, install packages, and add environment variables.

To start using the xpander SDK in your AI Agent code, let’s set up a virtual environment.

1

Create a project and virtual environment

Create a new project directory and set up a Python virtual environment:

mkdir my_project
cd my_project
python -m venv .venv
2

Activate the virtual environment

Do this every time you start a new terminal session:

# On macOS/Linux
source .venv/bin/activate

# On Windows
.venv\Scripts\activate
3

Install the xpander SDK

Install the following packages:

pip install xpander-sdk
pip install openai
pip install dotenv
4

Set up environment variables in .env file

Grab these two API keys and the AI Agent ID and place them in your .env file:

  • OpenAI Key from https://platform.openai.com
  • Your AI Agent ID — by clicking the SDK trigger node at the top of the graph in the AI Agent Workbench
  • Your default API key From the same screen, also check the box next to the default API key and copy the API key

echo "XPANDER_API_KEY=<your_key>" >> .env
echo "OPENAI_API_KEY=<your_key>" >> .env  
echo "XPANDER_AGENT_ID=<your_agent_id>" >> .env  

Make sure the three parameters are correctly added to the .env file.

AI Agent code setup

1

Load packages and environment variables, and initialize the clients

Create your agent.py file, and let’s start building it step by step. The following code will load the packages and keys, and initialize the clients:

from xpander_sdk import XpanderClient
from openai import OpenAI
from dotenv import load_dotenv
from os import environ

load_dotenv()
OPENAI_API_KEY = environ["OPENAI_API_KEY"]
XPANDER_API_KEY = environ["XPANDER_API_KEY"]
XPANDER_AGENT_ID = environ["XPANDER_AGENT_ID"]

# Initialize clients
xpander_client = XpanderClient(api_key=XPANDER_API_KEY)
openai_client = OpenAI(api_key=OPENAI_API_KEY)

# Get your agent
agent = xpander_client.agents.get(agent_id=XPANDER_AGENT_ID)
2

Create a task and initialize state

This code creates an immutable task for the agent. Tasks are immutable by design, allowing you to track state changes throughout execution:

# Create a task for the agent
task1 = agent.add_task("""
Summarize the last 24 hours top hackernews posts
""")
3

Add the AI Agent loop

Add the following code to your agent.py file.

# Run the agent until completion
while not agent.is_finished():
    # Get LLM response with available tools
    response = openai_client.chat.completions.create(
        model="gpt-4o",
        messages=agent.messages,
        tools=agent.get_tools(),
        tool_choice=agent.tool_choice,
        temperature=0.0
    )
            
    # Process LLM response
    agent.add_messages(response.model_dump())
    
    # Extract and run tool calls
    tool_calls = XpanderClient.extract_tool_calls(llm_response=response.model_dump())
    agent.run_tools(tool_calls=tool_calls)

The is_finished() API checks the execution state of the agent, enabling you to control the execution loop programmatically. This approach gives you full visibility into the agent’s execution process.

4

Retrieve execution results

Once execution is complete, this code retrieves the immutable execution result:

# Get final results
execution_result = agent.retrieve_execution_result()
print("Status:", execution_result.status)
print("Result:", execution_result.result)

The execution result is immutable, meaning it can be safely retrieved at any point during or after execution without affecting the agent’s state. This design allows you to monitor progress or record results while the agent continues processing.

5

Run the agent.py file in your IDE or via the command line by running python agent.py

Example output:

# execution_result.status
Status: ExecutionStatus.COMPLETED

# execution_result.result
Result: Here are the top Hacker News stories from the last 24 hours:

1. Show HN: Nash, I made a standalone note with a single HTML file
   - Author: yevgenyhong
   - Score: 204
   - Comments: 47
   - Summary: A user has created a standalone note application using a single HTML file. This tool does not require any membership or software installation and can be used both online and offline. It is suitable for sharing content through messengers like Telegram and can also be used for hosting and blogging.
   - Link: [Read more](https://keepworking.github.io/nash/)

2. Finding Signal in the Noise: Machine Learning and the Markets (Jane Street)
   - Author: lewiscarson
   - Score: 76
   - Comments: 32
   - Summary: An article discussing the application of machine learning in financial markets, focusing on how to find meaningful signals amidst the noise.
   - Link: [Read more](https://signalsandthreads.com/finding-signal-in-the-noise/)

3. Sign in as anyone: Bypassing SAML SSO authentication with parser differentials
   - Author: campuscodi
   - Score: 200
   - Comments: 73
   - Summary: A security blog post detailing a method to bypass SAML SSO authentication using parser differentials, highlighting a significant security vulnerability.
   - Link: [Read more](https://github.blog/security/sign-in-as-anyone-bypassing-saml-sso-authentication-with-parser-differentials/)

These stories highlight a range of topics from innovative tech projects to significant security insights.

You successfully used the xpander SDK to run an AI Agent using the configuration you designed in the AI Agent Workbench!

Troubleshooting / FAQ