Skip to main content

Building a Single Agent

This guide walks you through creating your first AI agent with PraisonAI.

Prerequisites

pip install praisonaiagents
Set your API key:
export OPENAI_API_KEY="your-api-key"

Basic Agent

from praisonaiagents import Agent

# Create a simple agent
agent = Agent(
    name="Assistant",
    instructions="You are a helpful assistant."
)

# Run the agent
result = agent.start("Hello! What can you help me with?")
print(result)

Agent with Custom Role

from praisonaiagents import Agent

agent = Agent(
    name="Researcher",
    role="Senior Research Analyst",
    goal="Provide accurate and comprehensive research",
    backstory="You are an experienced researcher with expertise in AI and technology.",
    instructions="Always cite sources and provide balanced perspectives."
)

result = agent.start("What are the latest trends in AI?")
print(result)

Agent with Tools

from praisonaiagents import Agent
from praisonaiagents.tools import internet_search

agent = Agent(
    name="Web Researcher",
    instructions="Search the web to find accurate information.",
    tools=[internet_search]
)

result = agent.start("Find the latest news about OpenAI")
print(result)

Agent with Memory

from praisonaiagents import Agent, Memory

# Create memory instance
memory = Memory()

agent = Agent(
    name="Assistant",
    instructions="You are a helpful assistant with memory.",
    memory=memory
)

# First conversation
agent.start("My name is Alice")

# Agent remembers the name
result = agent.start("What's my name?")
print(result)  # "Your name is Alice"

Agent Configuration Options

ParameterTypeDescription
namestrAgent name
rolestrAgent’s role description
goalstrWhat the agent aims to achieve
backstorystrBackground context for the agent
instructionsstrSystem instructions
modelstrLLM model to use (default: gpt-4o-mini)
toolslistList of tools available to the agent
memoryMemoryMemory instance for persistence
verboseboolEnable verbose logging

Using Different Models

from praisonaiagents import Agent

# OpenAI
agent = Agent(name="GPT Agent", model="gpt-4o")

# Anthropic
agent = Agent(name="Claude Agent", model="claude-3-5-sonnet-20241022")

# Ollama (local)
agent = Agent(name="Local Agent", model="ollama/llama3.2")

# Google
agent = Agent(name="Gemini Agent", model="gemini/gemini-2.0-flash")

Chat Mode

from praisonaiagents import Agent

agent = Agent(
    name="ChatBot",
    instructions="You are a friendly chatbot."
)

# Multi-turn conversation
messages = [
    {"role": "user", "content": "Hi!"},
    {"role": "assistant", "content": "Hello! How can I help?"},
    {"role": "user", "content": "Tell me a joke"}
]

result = agent.chat(messages)
print(result)

Next Steps