Model Integration: OpenCode Zen
Relevant source files
The following files were used as context for generating this wiki page:
- packages/console/app/src/routes/zen/util/handler.ts
- packages/console/app/src/routes/zen/v1/chat/completions.ts
- packages/console/app/src/routes/zen/util/provider/provider.ts
- packages/console/core/src/provider.ts
- packages/console/app/src/routes/zen/index.tsx
- packages/opencode/src/session/prompt/gemini.txt
- packages/opencode/src/session/prompt/codex.txt
- README.md
OpenCode Zen is a service that provides access to a curated set of reliable, optimized AI models specifically tested and benchmarked for coding agents. It is designed to address the challenge of inconsistent performance and quality from various model providers, ensuring that developers have access to validated models that are effective for software engineering tasks. While integrated with the OpenCode agent, Zen can be used with any agent.
Zen functions as a not-for-profit gateway, passing through costs from model providers directly to the user. It simplifies the process of using powerful models from providers like Anthropic, Google, and OpenAI by offering a unified API endpoint and handling authentication, billing, and provider-specific interactions. This allows developers to focus on building with agents without managing multiple API keys and billing accounts.
Sources: packages/console/app/src/routes/zen/index.tsx:30-34, packages/console/app/src/routes/zen/index.tsx:210-216, README.md:90-93
Agent Personas and System Prompts
A core aspect of how OpenCode utilizes models, including those accessed via Zen, is through detailed system prompts that define an agent's persona, capabilities, and operational guidelines. These prompts are extensive instruction sets that configure the agent to perform specific tasks, such as general software engineering, creating new applications, or conducting research.
These prompts enforce strict operational mandates:
- Adherence to Conventions: Agents must rigorously follow the coding style, libraries, and architectural patterns of the existing project.
- Verification: Agents are instructed to verify changes by running project-specific build, linting, and test commands.
- Safety: Agents must explain critical commands before execution and never expose sensitive information like API keys.
- Tool Usage: Specific rules govern how agents use tools for file system operations, command execution, and web research.
Sources: packages/opencode/src/session/prompt/gemini.txt:3-23, packages/opencode/src/session/prompt/codex.txt:4-15
The table below summarizes some of the available agent prompts and their primary focus.
| Prompt File | Primary Focus & Key Mandates |
|---|---|
gemini.txt | General software engineering tasks. Core mandates include adhering to project conventions, mimicking existing style, and verifying changes with tests and linters. |
codex.txt | A coding agent persona with a focus on concise, direct communication. It introduces the concept of AGENTS.md for repository-specific instructions and detailed planning using a todowrite tool. |
beast.txt | An autonomous agent persona designed for extensive internet research using a webfetch tool. It emphasizes a deep understanding of the problem and incremental implementation. |
anthropic.txt | An interactive CLI tool persona. Emphasizes conciseness, task management with TodoWrite tools, and proactive use of specialized sub-agents. |
qwen.txt | An interactive CLI tool persona with a strong emphasis on conciseness (fewer than 4 lines of text) and security, refusing to work on malicious code. |
polaris.txt | A coding agent persona that prioritizes professional objectivity, task management, and using specialized sub-agents for exploration. |
generate.txt | A meta-agent persona. This agent is an "elite AI agent architect" designed to create other agent configurations (personas and system prompts) based on user requirements. |
Sources: packages/opencode/src/session/prompt/gemini.txt, packages/opencode/src/session/prompt/codex.txt, packages/opencode/src/session/prompt/beast.txt, packages/opencode/src/session/prompt/anthropic.txt, packages/opencode/src/session/prompt/qwen.txt, packages/opencode/src/session/prompt/polaris.txt, packages/opencode/src/session/prompt/generate.txt
System Architecture
Zen acts as a unified gateway or proxy between an AI agent (like OpenCode) and various third-party LLM providers. It exposes a single API endpoint that abstracts the complexity of interacting with different provider APIs.
The following diagram illustrates the high-level data flow.
API Endpoint: Chat Completions
The primary entry point for the Zen service is a POST endpoint that mimics the OpenAI Chat Completions API format for compatibility.
POST /zen/v1/chat/completions
This endpoint accepts a request body containing the model to use and the message history, then streams the response from the selected backend provider.
Sources: packages/console/app/src/routes/zen/v1/chat/completions.ts:10-12
Request Body
| Parameter | Type | Description | Required |
|---|---|---|---|
model | string | The identifier for the desired model (e.g., claude-3-opus-20240229). | Yes |
messages | Array<Message> | An array of message objects representing the conversation history. | Yes |
stream | boolean | If set to true, the response will be streamed. Zen's handler forces this to true. | No |
Source: packages/console/app/src/routes/zen/util/handler.ts:31-34
Request Handling Flow
The handler function manages the entire lifecycle of an incoming API request.
The sequence diagram below shows the detailed interaction between the components.
The key steps are:
- Authentication: The handler first authenticates the request to identify the user and retrieve their current balance.
- Validation: It validates the incoming JSON body against a Zod schema to ensure
modelandmessagesare correctly formatted. - Provider Discovery: It finds the appropriate provider instance based on the
modelstring in the request. - Streaming: It invokes the
streammethod on the selected provider, which establishes a connection to the backend LLM and pipes the response back to the client.
Sources: packages/console/app/src/routes/zen/util/handler.ts:15-54
Provider Abstraction
To support multiple LLM backends, Zen uses a provider abstraction layer. A generic Provider interface defines the contract that each provider-specific implementation must adhere to.
The core of this abstraction is the Provider type, which includes metadata and a stream function.
// Source: packages/console/app/src/routes/zen/util/provider/provider.ts:24-31
export type Provider = {
id: string;
name: string;
models: string[];
stream: (
body: any,
options: {
headers: Headers;
}
) => Promise<ReadableStream>;
};
This design allows the system to be extensible. New models or providers can be added by creating a new Provider object and registering it. The find function in the provider module is responsible for looking up the correct provider based on the model ID requested by the client.
Sources: packages/console/app/src/routes/zen/util/provider/provider.ts:24-38, [packages/console/core/src/provider.ts]
Conclusion
OpenCode Zen provides a robust and simplified gateway to a curated suite of AI models optimized for coding. By abstracting away the complexities of individual provider APIs and managing authentication and billing, it allows developers to seamlessly integrate powerful, reliable AI capabilities into their agents. The architecture, centered around a unified API endpoint and an extensible provider model, ensures both ease of use and future flexibility. The use of detailed system prompts further refines the behavior of these models, tailoring them for high-quality software engineering tasks.