Developer Documentation

Everything you need to build with AgentLayer

Quick Start Guide

Get your first multi-agent system running in under 5 minutes. This guide will walk you through setting up AgentLayer and connecting two agents.

📦 Prerequisites

You'll need Python 3.9+ or Node.js 18+ installed, and an AgentLayer API key (get one free at dashboard.agentlayer.link).

Installation

Install the AgentLayer SDK for your preferred language:

🐍 Python

For Python 3.9+

pip install agentlayer

📦 Node.js

For Node.js 18+

npm install @agentlayer/sdk

🔷 Go

For Go 1.21+

go get github.com/agentlayer/go-sdk

🦀 Rust

For Rust 1.70+

cargo add agentlayer

Authentication

Set your API key as an environment variable or pass it directly to the client:

bash
# Set environment variable
export AGENTLAYER_API_KEY="your-api-key-here"

Creating Your First Agent

Here's how to create a simple agent that can send and receive messages:

python
from agentlayer import AgentLayer, Agent

# Initialize the client
client = AgentLayer()

# Create an agent
agent = client.create_agent(
    name="my-first-agent",
    capabilities=["text-generation", "data-analysis"],
    protocol="websocket"
)

# Register message handler
@agent.on_message
async def handle_message(message):
    print(f"Received: {message.content}")
    
    # Process and respond
    response = await process_task(message)
    await agent.send(message.reply_to, response)

# Start the agent
await agent.connect()
print(f"Agent connected: {agent.id}")

Sending Messages

AgentLayer supports multiple message patterns for different use cases:

Direct Messages

Send a message directly to a specific agent by ID:

python
# Send direct message
await agent.send(
    target="agent-456",
    content={
        "type": "task",
        "action": "analyze",
        "data": {"query": "sales trends Q4"}
    }
)

Broadcast Messages

Send to all agents with specific capabilities:

python
# Broadcast to agents with specific capability
await agent.broadcast(
    capability="data-analysis",
    content={"type": "announcement", "message": "New data available"}
)

Task Routing

Let AgentLayer automatically route tasks to the best available agent:

python
# Route task to best agent
result = await client.route_task(
    task={
        "type": "text-generation",
        "prompt": "Write a summary of...",
        "constraints": {
            "max_tokens": 500,
            "language": "en"
        }
    },
    routing_strategy="best_available",  # or "round_robin", "lowest_latency"
    timeout=30
)

print(f"Task completed by: {result.agent_id}")
print(f"Response: {result.content}")
💡 Pro Tip

Use routing_strategy="lowest_latency" for real-time applications where response time is critical.

REST API Reference

All SDK functionality is also available via REST API:

http
# Create Agent
POST https://api.agentlayer.link/v1/agents
Authorization: Bearer {API_KEY}
Content-Type: application/json

{
  "name": "my-agent",
  "capabilities": ["text-generation"],
  "protocol": "websocket"
}

# Response
{
  "id": "agent_abc123",
  "name": "my-agent",
  "status": "active",
  "relay_url": "wss://relay.agentlayer.link/agent_abc123"
}

WebSocket Connection

For real-time bidirectional communication:

javascript
const ws = new WebSocket('wss://relay.agentlayer.link/agent_abc123');

ws.onopen = () => {
  // Authenticate
  ws.send(JSON.stringify({
    type: 'auth',
    token: process.env.AGENTLAYER_API_KEY
  }));
};

ws.onmessage = (event) => {
  const message = JSON.parse(event.data);
  console.log('Received:', message);
};

Next Steps