Get Started with Python
This guide will walk you through creating and deploying your first AI agent using the aiXplain Python SDK. You'll learn how to specify a Large Language Model (LLM), equip the agent with tools, and integrate the agent into your application.
Setup
Set an aiXplain API Key
Generate an API key from the Integrations page. Once generated, you can use it in one of two ways:
- Export it as an environment variable in your terminal or
- Set it in your Python project using either environment variables or a
.env
file withpython-dotenv
.
- MacOS / Linux
- Windows
export AIXPLAIN_API_KEY="<KEY>"
setx AIXPLAIN_API_KEY="<KEY>"
- os module
- .env (via dotenv)
import os
os.environ["AIXPLAIN_API_KEY"] = "<KEY>"
from dotenv import find_dotenv, load_dotenv # pip install python-dotenv
load_dotenv(find_dotenv()) # Load environment variables from .env file
Install the aiXplain SDK
pip install aixplain
Create an AI Agent
Agents are defined using a name
, description
, and instructions
.
from aixplain.factories import AgentFactory
post_agent = AgentFactory.create(
name="Post Generating Agent",
description="An agent that creates social media posts based on a given topic.",
instructions="You generate engaging, platform-ready social media posts complete with suggested images. Keep the tone informative and audience-focused."
)
Only the instructions
field influences an agent's behavior. The name
and description
are metadata and do not affect execution.
aiXplain agents are currently optimized for GPT-4o Mini (default) and Llama 3.1. However, you can override the default llm_id
with your preferred model.
Explore the marketplace to choose from over 170 LLMs.
Run the agent
response = post_agent.run("What's an AI agent?")
response.data.output
Enable conversational memory
Retrieve the session_id
from the first query and include it in subsequent requests to maintain the agent’s conversation history.
session_id = response.data.session_id
response = post_agent.run("What makes this text interesting?", session_id=session_id)
response.data.output
Deploy the agent
Creating an agent with create
generates a draft agent with a temporary API endpoint that expires after 24 hours. Draft agents are ideal for development and debugging.
To make an agent permanent, you need to deploy it. By default, agents are deployed on aiXplain’s multi-tenant AWS cloud environment.
post_agent.deploy()
Add tools
The agentic framework supports different types of tools. This section demonstrates how to list, try and add an AI model as an agent tool.
List models
from aixplain.factories import ModelFactory
from aixplain.enums import Function
# List utility models
model_list = ModelFactory.list(function=Function.UTILITIES, page_size=50)["results"]
for model in model_list:
print(model.id, model.name, model.supplier)
Alternatively, browse models and other assets in the marketplace.
Try the model
In this example, we will use the Google Search by Scale SERP model.
model = ModelFactory.get("65c51c556eb563350f6e1bb1") # or model_list[#]
response = model.run("Latest news about AI agents")
response.data.output
Add to the agent
First convert the model asset into a tool. Then add it to the agent.
from aixplain.factories import AgentFactory
google_search_tool = AgentFactory.create_model_tool(model="65c51c556eb563350f6e1bb1")
post_agent.tools.append(google_search_tool)
Create a team agent
A Team Agent orchestrates multiple agents to execute multi-step plans collaboratively.
from aixplain.factories import TeamAgentFactory
team_agent = TeamAgentFactory.create(
name="Social Media Agent",
description="An AI-powered agent that generates engaging social media posts, complete with suggested images and SEO-optimized captions.",
agents=[post_agent]
)
# Run the team agent for the first time
# response = team_agent.run("Generate a post about eco-friendly travel.")
# Use the same session ID to maintain conversational context
# team_agent.run("Make it more concise.", session_id=response.data.session_id)
# Deploy the team agent to make it production-ready
# team_agent.deploy()
Next – Customize your agent
Now that you’ve seen how agents can collaborate as a team, let’s dive deeper into how you can customize individual agents even further. From refining instructions and selecting tools to configuring memory, guardrails, and model behavior—agents on aiXplain are highly adaptable to your specific use case.