OpenCode
OpenCode and OpenCode Zen expose models through several API formats. Choose the format that matches the model or gateway, then use the resulting provider like any other nimgent provider.
Create a model
Section titled “Create a model”This example uses the default Chat Completions format.
import std/osimport nimgentimport nimgent/providers/openai
let model = openCode( getEnv("OPENCODE_API_KEY"), protocol = ocChat).model(getEnv("OPENCODE_MODEL"))
echo generateText(model, prompt = "Say hello in one sentence.").textSet OPENCODE_API_KEY and OPENCODE_MODEL before you run the program. The repository does not define an OpenCode model ID, so use one from the OpenCode catalog.
Choose the request format
Section titled “Choose the request format”Set protocol to match the model’s gateway format:
| Protocol | Use when the gateway expects |
|---|---|
ocChat |
OpenAI Chat Completions. This is the default. |
ocResponses |
OpenAI Responses. |
ocMessages |
Anthropic Messages. |
ocGoogle |
Native Google Gemini paths. |
For OpenCode Zen, use openCodeZen with the same protocol choices:
let model = openCodeZen( getEnv("OPENCODE_API_KEY"), protocol = ocResponses).model(getEnv("OPENCODE_MODEL"))If a request fails, confirm that the selected protocol matches the model’s gateway. ocGoogle is for Gemini models served on native Google API paths.
Next steps
Section titled “Next steps”See Settings for portable controls, or Middleware and routing to choose providers from a model ID.