Overview
The SDK allows you to configure OpenCode programmatically. You can pass configuration when creating a server instance, or update it at runtime via the API.
Initial Configuration
Pass configuration when creating an OpenCode instance:
import { createOpencode } from '@opencode-ai/sdk'
const { client, server } = await createOpencode({
config: {
model: 'anthropic/claude-3-5-sonnet-20241022',
logLevel: 'INFO',
theme: 'dark',
agent: {
build: {
model: 'anthropic/claude-3-5-sonnet-20241022',
temperature: 0.7,
},
},
},
})
The configuration is passed via the OPENCODE_CONFIG_CONTENT environment variable and merged with any existing opencode.json file.
Runtime Configuration
Update configuration at runtime:
// Get current configuration
const config = await client.config.get()
console.log(config.data.model)
// Update configuration
await client.config.update({
body: {
model: 'anthropic/claude-3-5-sonnet-20241022',
logLevel: 'DEBUG',
},
})
Configuration Options
Model Selection
Default model to use (format: provider/model)config: {
model: 'anthropic/claude-3-5-sonnet-20241022'
}
Small model for tasks like title generationconfig: {
small_model: 'anthropic/claude-3-haiku-20240307'
}
Logging
logLevel
'DEBUG' | 'INFO' | 'WARN' | 'ERROR'
default:"INFO"
Log level for server outputconfig: {
logLevel: 'DEBUG'
}
Agent Configuration
agent
Record<string, AgentConfig>
Configure individual agentsconfig: {
agent: {
build: {
model: 'anthropic/claude-3-5-sonnet-20241022',
temperature: 0.7,
maxSteps: 20,
permission: {
edit: 'allow',
bash: 'ask',
},
},
plan: {
model: 'anthropic/claude-3-opus-20240229',
temperature: 0.5,
},
},
}
AgentConfig Properties
Model for this agent (overrides global model)
Temperature for model sampling (0-1)
Maximum agentic iterations before forcing text-only response
Description of when to use this agent
mode
'subagent' | 'primary' | 'all'
default:"all"
When this agent is available
Hex color code for the agent (e.g., #FF5733)
Custom system prompt for this agent
Enable/disable specific toolstools: {
bash: true,
webfetch: false,
}
Permission settingspermission: {
edit: 'allow', // File editing
bash: 'ask', // Shell commands
webfetch: 'allow', // Web fetching
doom_loop: 'deny', // Prevent infinite loops
}
Values: 'ask' | 'allow' | 'deny'
Provider Configuration
provider
Record<string, ProviderConfig>
Configure custom providers or override defaultsconfig: {
provider: {
anthropic: {
options: {
apiKey: 'your-api-key',
baseURL: 'https://api.anthropic.com',
},
},
custom: {
api: 'https://api.example.com',
name: 'Custom Provider',
models: {
'my-model': {
id: 'my-model-id',
name: 'My Model',
cost: { input: 0.01, output: 0.03 },
limit: { context: 128000, output: 4096 },
},
},
},
},
}
MCP Servers
mcp
Record<string, McpConfig>
Configure Model Context Protocol serversconfig: {
mcp: {
filesystem: {
type: 'local',
command: ['npx', '-y', '@modelcontextprotocol/server-filesystem', '/path'],
enabled: true,
},
github: {
type: 'remote',
url: 'https://mcp.example.com',
enabled: true,
},
},
}
Commands
command
Record<string, CommandConfig>
Define custom commandsconfig: {
command: {
review: {
template: 'Review the code in {{0}} for issues',
description: 'Code review',
agent: 'build',
},
test: {
template: 'Write tests for {{0}}',
description: 'Generate tests',
subtask: true,
},
},
}
Plugins
Load custom pluginsconfig: {
plugin: [
'./plugins/custom-tool.ts',
'@company/opencode-plugin',
],
}
TUI Settings
Terminal UI settingsconfig: {
tui: {
scroll_speed: 3,
scroll_acceleration: { enabled: true },
diff_style: 'auto',
},
}
Theme nameconfig: {
theme: 'dark'
}
Sharing
share
'manual' | 'auto' | 'disabled'
default:"manual"
Session sharing behaviorconfig: {
share: 'auto' // Auto-share all sessions
}
Other Options
Custom username for display
Additional instruction files to includeconfig: {
instructions: ['STYLE_GUIDE.md', '.cursorrules']
}
Global tool enable/disableconfig: {
tools: {
bash: true,
webfetch: true,
},
}
Global permission settings (can be overridden per agent)
Examples
Development Configuration
const { client, server } = await createOpencode({
config: {
logLevel: 'DEBUG',
model: 'anthropic/claude-3-5-sonnet-20241022',
agent: {
build: {
permission: {
edit: 'allow',
bash: 'allow',
},
},
},
},
})
Production Configuration
const { client, server } = await createOpencode({
config: {
logLevel: 'ERROR',
model: 'anthropic/claude-3-5-sonnet-20241022',
share: 'disabled',
permission: {
edit: 'ask',
bash: 'ask',
webfetch: 'ask',
},
},
})
Custom Provider
const { client, server } = await createOpencode({
config: {
provider: {
'my-provider': {
api: 'https://api.example.com/v1',
name: 'My Provider',
options: {
apiKey: process.env.API_KEY,
},
models: {
'my-model': {
id: 'my-model-v1',
name: 'My Custom Model',
cost: { input: 0.01, output: 0.03 },
limit: { context: 100000, output: 4096 },
},
},
},
},
model: 'my-provider/my-model',
},
})
Multi-Agent Setup
const { client, server } = await createOpencode({
config: {
agent: {
architect: {
model: 'anthropic/claude-3-opus-20240229',
description: 'High-level architecture and design',
temperature: 0.3,
mode: 'primary',
},
builder: {
model: 'anthropic/claude-3-5-sonnet-20241022',
description: 'Implementation and coding',
temperature: 0.7,
mode: 'all',
permission: {
edit: 'allow',
bash: 'ask',
},
},
tester: {
model: 'anthropic/claude-3-5-sonnet-20241022',
description: 'Testing and validation',
temperature: 0.5,
mode: 'subagent',
},
},
},
})
Retrieve information about available providers and models:
// List all providers and their default models
const info = await client.config.providers()
console.log('Available providers:')
for (const provider of info.data.providers) {
console.log(`- ${provider.name} (${provider.id})`)
console.log(` Models: ${Object.keys(provider.models).length}`)
}
console.log('\nDefaults:', info.data.default)
Type Safety
All configuration is fully typed:
import type { Config, AgentConfig } from '@opencode-ai/sdk'
const config: Config = {
model: 'anthropic/claude-3-5-sonnet-20241022',
agent: {
build: {
temperature: 0.7,
maxSteps: 20,
} satisfies AgentConfig,
},
}
const { client, server } = await createOpencode({ config })