Skip to content

Examples

This section provides working examples demonstrating various SDK features.

Documentation

Resource Description
Example Files Explained Detailed line-by-line explanations of each example file
Recipes Copy-paste code snippets for common patterns

Quick Start Examples

Example Description Difficulty
Basic Agent Create your first agent Beginner
Tool Integration Add tools to agents Intermediate
RAG Document Q&A Query documents with Knowledge Base Intermediate
Agent Orchestration Multi-agent coordination patterns Advanced
Multi-Agent Orchestrate multiple agents Advanced
Guardrails Add safety controls Intermediate

Running Examples

Prerequisites

# Install dependencies
poetry install

# Set environment variables
export AWS_REGION=us-east-1  # or your preferred region
export CHAT_SESSIONS_TABLE_NAME=your-sessions-table
export CHAT_MESSAGES_TABLE_NAME=your-messages-table

# Optional: For weather tool examples
export WEATHER_API_KEY=your-weather-api-key

Available Examples

All examples are in the examples/ directory:

# List examples
ls examples/*.py

Example Categories

1. Basic Agents

Create simple agents with different configurations:

from akordi_agents.core import create_langgraph_agent
from akordi_agents.services import AWSBedrockService

agent = create_langgraph_agent(
    name="my_agent",
    llm_service=AWSBedrockService(),
)
from akordi_agents.core import AgentBuilder

agent = (
    AgentBuilder("my_agent")
    .with_llm_service_instance(AWSBedrockService())
    .with_config({"temperature": 0.1})
    .build()
)

2. Document Q&A (RAG)

Query documents using knowledge base search. See RAG Document Q&A for full documentation.

poetry run python examples/talk_to_document.py \
  --query "What is this document about?" \
  --knowledge_base_id "YOUR_KB_ID"
poetry run python examples/langgraph_ttd.py \
  --query "What is this document about?" \
  --knowledge_base_id "YOUR_KB_ID"
from akordi_agents.core import create_langgraph_agent
from akordi_agents.handlers import AWSBedrockSearchHandler

agent = create_langgraph_agent(
    name="rag_agent",
    llm_service=llm_service,
    search_handler=AWSBedrockSearchHandler(
        knowledge_base_id="your-kb-id"
    ),
)

3. Tool Integration

Use agents with custom tools:

# Simple tool example (without LangGraph)
poetry run python examples/simple_tool.py --query "What's the weather in London?"

# LangGraph tool workflow
poetry run python examples/lang_tool.py --query "What's the weather in Tokyo?"

# LangGraph with simple tool
poetry run python examples/langgraph_with_simple_tool.py
from akordi_agents.core import create_langgraph_agent
from akordi_agents.tools import Tool

class WeatherTool(Tool):
    def get_name(self) -> str:
        return "weather_tool"

    def get_description(self) -> str:
        return "Get current weather for a city"

    def get_input_schema(self) -> dict:
        return {
            "type": "object",
            "properties": {
                "city": {"type": "string", "description": "City name"}
            },
            "required": ["city"]
        }

    def execute(self, **kwargs) -> dict:
        city = kwargs.get("city", "Unknown")
        return {"temperature": "22°C", "conditions": "Sunny"}

agent = create_langgraph_agent(
    name="weather_agent",
    llm_service=llm_service,
    tools=[WeatherTool()],
    config={"enable_tools": True}
)

4. Multi-Agent Systems

Coordinate multiple specialized agents. See Agent Orchestration for full documentation.

# Agent-to-Agent flow
poetry run python examples/agent_to_agent_flow.py \
  --query "Analyze construction risks in London weather"

# LangGraph orchestration with multiple patterns
poetry run python examples/agent_orchestration_langgraph.py \
  --query "Assess weather and financial risks"

# Specific orchestration pattern
poetry run python examples/agent_orchestration_langgraph.py \
  --pattern coordinator \
  --query "What is the weather in London? Assess construction risks."

Orchestration patterns available:

Pattern Description
coordinator Central coordinator delegates to specialists
peer_to_peer Agents communicate directly
hierarchical Multi-level delegation hierarchy

5. Guardrails

Safety controls with AWS Bedrock:

# Create guardrail
poetry run python examples/create_guardrail.py --create-default

# Use guardrails
poetry run python examples/agent_with_guardrails.py \
  --query "Assess risks of working at heights"

# Agent without guardrails (for comparison)
poetry run python examples/agent_without_guardrails.py

6. Chat Management

Store and retrieve chat sessions with DynamoDB:

poetry run python examples/agent_chat_tool.py --example direct

7. Full Stack Demo

Web application with React frontend and FastAPI backend:

# Start backend (Terminal 1)
poetry run python examples/agent_demo_backend.py

# Start frontend (Terminal 2)
cd examples/frontend && npm install && npm start

Quick Reference

Example File Command Use Case Docs
talk_to_document.py poetry run python examples/talk_to_document.py Document Q&A (no LangGraph) RAG Doc
langgraph_ttd.py poetry run python examples/langgraph_ttd.py LangGraph document Q&A RAG Doc
agent_orchestration_langgraph.py poetry run python examples/agent_orchestration_langgraph.py Multi-agent orchestration Orchestration
simple_tool.py poetry run python examples/simple_tool.py Basic tools without LangGraph Tools
lang_tool.py poetry run python examples/lang_tool.py LangGraph tools Tools
langgraph_with_simple_tool.py poetry run python examples/langgraph_with_simple_tool.py Combined example Tools
agent_to_agent_flow.py poetry run python examples/agent_to_agent_flow.py A2A protocol Multi-Agent
create_guardrail.py poetry run python examples/create_guardrail.py Create AWS Bedrock guardrails Guardrails
agent_with_guardrails.py poetry run python examples/agent_with_guardrails.py Safe agents with guardrails Guardrails
agent_without_guardrails.py poetry run python examples/agent_without_guardrails.py Agent comparison Guardrails
agent_demo_backend.py poetry run python examples/agent_demo_backend.py FastAPI web backend -
draft_mail.py poetry run python examples/draft_mail.py Email generation -
risk_describe_agent.py poetry run python examples/risk_describe_agent.py Risk assessment -

Example Tools

Custom tools available in examples/tools/:

Tool Description File
WeatherTool Get real-time weather from WeatherAPI weather_tool.py
RiskAssessmentTool Assess project risks risk_assessment_tool.py
FinanceAnalysisTool Financial analysis finance_analysis_tool.py
HRRecruitmentTool HR and recruitment hr_recruitment_tool.py
ChatHistoryTool Chat management chat_history_tool.py

Environment Variables

Create a .env file based on examples/.env-example:

# AWS Configuration
AWS_REGION=us-east-1
AWS_PROFILE=default

# DynamoDB Tables (optional)
CHAT_SESSIONS_TABLE_NAME=akordi-chat-sessions
CHAT_MESSAGES_TABLE_NAME=akordi-chat-messages
AKORDI_TOKEN_USAGE_TABLE=akordi-token-usage

# Guardrails (optional)
GUARDRAIL_ID=your-guardrail-id
GUARDRAIL_VERSION=1

# External APIs (optional)
WEATHER_API_KEY=your-weather-api-key

# LangSmith Tracing (optional)
LANGCHAIN_TRACING_V2=true
LANGCHAIN_API_KEY=your-langsmith-api-key
LANGCHAIN_PROJECT=akordi-agents

# Logging
LOG_LEVEL=INFO

Next Steps