Module 4: Advanced Customization
Run your own agent loop, add tools and optionally deploy with Xpander CLI
Module Summary
- Goal: Customize agent loops, add Python tools, and run the agent locally or via cloud event hook
- Estimated Time: 45–60 minutes
- Prerequisites: Python 3.12, Node.js v22, completed Modules 1–3
🧭 What You’ll Do in This Module
In this module, you’ll gain full control over your agent’s behavior.
You’ll learn how to:
- Run your agent locally via Notebook, CLI, or event handler
- Customize the agent loop, allowing advanced logic or prompt shaping
- Add your own Python tools to expand what the agent can do
- Optionally deploy the agent using the xpander.ai CLI for cloud-based execution
By the end, you’ll be able to create and extend fully working agents tailored to your needs - whether you want to explore behavior in a notebook or power up a frontend integration.
Let’s get started.
⚙️ Environment Setup
This setup is required for all run modes (Notebook, CLI, Event-driven):
Tool | Required Version |
---|---|
Python | 3.12.7 |
Node.js | v22 |
Use pyenv
and nvm
to manage these versions if needed:
🧱 Step 1: Clone the Template Repository
🔐 Step 2: CLI Login & Agent Initialization
Install the CLI and initialize your agent:
This creates the following files:
xpander_config.json
agent_instructions.json
requirements.txt
📦 Step 3: Install Agent Dependencies
Fill in the required values in .env
:
💬 Step 4: Run the Agent
Option A: Notebook Mode (Recommended for Exploration)
This mode is ideal for stepping through the agent lifecycle.
▶️ 1. Open the Notebook
Open my_agent.ipynb
in VSCode, Jupyter Lab, or any notebook viewer.
🧪 2. Import Libraries
Run this cell first:
🔧 3. Load Configuration
🧠 4. Initialize and Wrap the Agent
💬 5. Send a Task to the Agent
📊 6. Retrieve and Display Result
🧾 7. Visualize Message History (Optional)
Option B: CLI Mode
Run the agent directly via:
Then send a prompt like:
Option C: Event-Driven Mode
Run the event handler script for live frontend integration:
🔧 Step 5: Add Local Tools
Define a custom tool in tools/local_tools.py
:
Register in my_agent.py
:
🧠 Step 6: Customize the Agent Loop
Modify the main loop in my_agent.py
:
Customize:
- Prompt rewriting
- Token usage tracking
- Advanced condition handling
- Custom logging or retry mechanisms
🚀 Step 7: Deploy to Cloud
Use Xpander CLI to deploy your agent:
View live logs:
🧯 Troubleshooting
Issue | Fix |
---|---|
Failed to sync agent | Re-check .env and xpander_config.json and re-run init |
add_graph_item errors | Confirm a clean tool graph and valid agent ID |
BrokenPipe / JSII issues | Restart shell or rebuild .venv |
No event triggers | Only run xpander_handler.py after triggering from frontend |
✅ Summary
Feature | Benefit |
---|---|
🧠 Custom Logic | Full control with _agent_loop() |
🛠 Tooling | Add Python tools as callable functions |
🧲 Hybrid Mode | Local + Cloud support |
📦 One-Line Deploy | xpander deploy ready |
🔍 Insights | Built-in metrics and logs |