Launch and manage OpenCode servers with simple commands, from single instances to multi-server swarms
Simple commands and scripts to launch and manage OpenCode servers, from single instances to multi-server swarms.
UNIVERSAL CAPABILITY: This skill launches servers for ANY agent type, not just the examples shown. The agent folder names (code-analyzer, documentation-writer, etc.) used in examples are for illustration ONLY. Real swarms will have completely different agent types and purposes. This skill works universally with any folder structure and any agent specialization.
IMPORTANT: Create custom agents in .opencode/agent/ BEFORE launching the server. Agents are only loaded when the server starts.
WRONG: timeout 300s opencode serve --port 3001 &
WRONG: timeout 300s curl http://localhost:3000/session/...
RIGHT: opencode serve --port 3001 &
RIGHT: curl http://localhost:3000/session/... (let complete naturally)
WRONG: opencode serve --port 3001 > /tmp/server.log 2>&1 &
RIGHT: opencode serve --port 3001 &
WRONG: cd folder && opencode serve --port 3001 > log.txt 2>&1 & PID=$!; echo "$PID" >> file
RIGHT: cd folder && opencode serve --port 3001 &
# Start server on random available port
opencode serve
# Start server on specific port
opencode serve --port 3000
# Start server on all interfaces
opencode serve --port 3000 --hostname 0.0.0.0
# Check server status
curl http://localhost:3000/config
# View available agents
curl http://localhost:3000/agent | jq .
# Check API documentation
curl http://localhost:3000/doc
# Server 1 - General purpose
opencode serve --port 3001 &
SERVER1_PID=$!
# Server 2 - Build focused
opencode serve --port 3002 &
SERVER2_PID=$!
# Server 3 - Documentation focused
opencode serve --port 3003 &
SERVER3_PID=$!
echo "Started servers: $SERVER1_PID, $SERVER2_PID, $SERVER3_PID"
For swarm deployment, launch servers in specific folders:
⚠️ EXAMPLE PATTERN ONLY - The folder names below are EXAMPLES. Use ANY folder names for your actual swarm:
# EXAMPLE: Launch server in [ANY_AGENT_FOLDER] folder
cd [agent_folder_name]
opencode serve --port 3001 &
SERVER1_PID=$!
cd ..
# EXAMPLE: Launch server in [DIFFERENT_AGENT_FOLDER] folder
cd [another_agent_folder]
opencode serve --port 3002 &
SERVER2_PID=$!
cd ..
# EXAMPLE: Launch server in [THIRD_AGENT_FOLDER] folder
cd [third_agent_folder]
opencode serve --port 3003 &
SERVER3_PID=$!
cd ..
UNIVERSAL TRUTH: The pattern works for ANY folder names:
marketing-specialist/ → cd marketing-specialistlegal-advisor/ → cd legal-advisorgame-developer/ → cd game-developerresearch-scientist/ → cd research-scientistmusic-composer/ → cd music-composerpersonal-trainer/ → cd personal-trainerREAL SWARMS WILL HAVE VASTLY DIFFERENT FOLDER NAMES - These examples only show the launching pattern.
Process Management:
echo "3001:[folder_name]:$SERVER1_PID" >> .opencode/server-pids.txtkill $SERVER1_PIDkill -0 $SERVER1_PIDNOTE: Replace [folder_name] with your actual folder names. The pattern port:folder:pid works for ANY folder names.
# Set log level
export OPENCODE_LOG_LEVEL=debug
# Set custom config directory
export OPENCODE_CONFIG_DIR=/path/to/config
# Set API keys for providers
export ANTHROPIC_API_KEY=your-key
export ZHIPU_API_KEY=your-key
OpenCode automatically loads configuration from:
~/.config/opencode/.opencode/ (in project root)Custom agents go in .opencode/agent/:
---
description: Specialized agent for documentation
mode: subagent
tools:
read: true
grep: true
permissions:
edit: allow
bash:
"git*": allow
"*": ask
---
You are an expert technical documentation writer...
curl -X POST http://localhost:3000/session \
-H "Content-Type: application/json" \
-d '{"title": "My Development Session"}'
curl -X POST http://localhost:3000/session/{session_id}/message \
-H "Content-Type: application/json" \
-d '{
"agent": "general",
"model": {"providerID": "zai-coding-plan", "modelID": "glm-4.6"},
"parts": [{"type": "text", "text": "Help me understand this codebase"}]
}'
curl http://localhost:3000/session | jq '.'
curl "http://localhost:3000/file/content?path=README.md"
# Check multiple servers
for port in 3001 3002 3003; do
if curl -s "http://localhost:$port/config" > /dev/null; then
echo "✅ Server on port $port - HEALTHY"
else
echo "❌ Server on port $port - DOWN"
fi
done
for port in 3001 3002 3003; do
if curl -s "http://localhost:$port/config" > /dev/null; then
sessions=$(curl -s "http://localhost:$port/session" | jq '. | length')
echo "Port $port: $sessions active sessions"
fi
done
# Dockerfile
FROM node:18-alpine
RUN npm install -g opencode-ai
EXPOSE 3000
CMD ["opencode", "serve", "--port", "3000", "--hostname", "0.0.0.0"]
# docker-compose.yml
version: '3.8'
services:
opencode-1:
build: .
ports:
- "3001:3000"
environment:
- OPENCODE_LOG_LEVEL=info
volumes:
- ./config:/app/.opencode
opencode-2:
build: .
ports:
- "3002:3000"
environment:
- OPENCODE_LOG_LEVEL=info
volumes:
- ./config:/app/.opencode
# Use API keys
export OPENCODE_API_KEY=your-secure-key
# Restrict access to localhost
opencode serve --hostname 127.0.0.1
# Use reverse proxy for SSL
# Configure nginx/cloudflare for HTTPS
# Set appropriate log levels
export OPENCODE_LOG_LEVEL=warn
# Monitor server resources
curl http://localhost:3000/config | jq '.providers'
# Find available port
python -c "import socket; s=socket.socket(); s.bind(('', 0)); print(s.getsockname()[1]); s.close()"
# Kill process using port
lsof -ti:3000 | xargs kill -9
# Check if port is available
netstat -tulpn | grep :3000
# Kill existing server
pkill -f "opencode serve"
# Test server response
curl -v http://localhost:3000/config
This skill provides everything needed to launch, configure, and manage OpenCode servers from basic single instances to multi-server swarms, all using simple commands and tools that are already available.