Skip to content

railwayapp-templates/multi-agent-system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multi-Agent System

Deploy multiple AI agents as separate Railway services that communicate via Redis and Postgres.

Deploy on Railway

Architecture

An orchestrator receives HTTP requests and dispatches tasks to agent-specific Redis queues. Each agent pulls from its queue, calls OpenAI, writes results to Postgres, and optionally pushes a follow-up task to the next agent's queue.

HTTP request → Orchestrator → Redis queue:researcher → Researcher Agent
                                                            ↓
                                              Postgres ← results
                                                            ↓
                                              Redis queue:writer → Writer Agent
                                                                       ↓
                                                         Postgres ← article

Services Deployed

Service Description
orchestrator FastAPI HTTP service that receives tasks and dispatches to Redis queues
researcher Worker that researches a topic via OpenAI and passes findings to the writer
writer Worker that turns research into a finished article via OpenAI
init One-shot service that creates the tasks table in Postgres
PostgreSQL Stores task state, agent outputs, and final results
Redis Message queue between services

Environment Variables

Variable Description Required
OPENAI_API_KEY Your OpenAI API key Yes
STARTUP_DELAY_SECONDS Seconds to wait before polling (researcher: 0, writer: 5) No

Variables DATABASE_URL and REDIS_URL are automatically set by Railway when you reference the database services.

How It Works

  1. POST /tasks?topic=quantum+computing hits the orchestrator
  2. Orchestrator creates a task in Postgres and pushes it to queue:researcher
  3. Researcher pulls the task, calls OpenAI for research, writes results to Postgres, pushes a new task to queue:writer
  4. Writer pulls the task, calls OpenAI to produce an article, writes the final output to Postgres
  5. GET /tasks/{task_id} returns the completed result

Getting Started

After deploying, send a task to the orchestrator:

curl -X POST "https://your-orchestrator.up.railway.app/tasks?topic=quantum+computing"

Check the result (replace TASK_ID with the returned ID):

curl "https://your-orchestrator.up.railway.app/tasks/TASK_ID"

Local Development

# Start Postgres and Redis locally
docker run -d --name pg -e POSTGRES_PASSWORD=postgres -p 5432:5432 postgres
docker run -d --name redis -p 6379:6379 redis

# Set environment variables
export DATABASE_URL=postgresql://postgres:postgres@localhost:5432/postgres
export REDIS_URL=redis://localhost:6379
export OPENAI_API_KEY=your-key

# Run the init script
python init/main.py

# Start services in separate terminals
uvicorn orchestrator.app:app --host 0.0.0.0 --port 8000
python researcher/worker.py
python writer/worker.py

About

Deploy multiple AI agents as separate Railway services that communicate via Redis and Postgres

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages