providers.mdx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. ---
  2. title: "Providers"
  3. description: ""
  4. ---
  5. OpenCode builds its provider and model catalog from [Models.dev](https://models.dev), then applies the `providers`
  6. overlays from your [configuration](/config). A provider needs both a usable runtime package and, when required, an
  7. active connection.
  8. ## Connect a provider
  9. Run `/connect` in the TUI, choose an integration, and complete one of the methods it offers:
  10. ```text
  11. /connect
  12. ```
  13. An integration may support an API key, OAuth, environment variables, or a combination of them. API keys and OAuth
  14. tokens entered through `/connect` are stored by the OpenCode service in its database. Run `/connect` again to replace
  15. or remove a stored credential.
  16. Providers from Models.dev also declare their standard environment variables. A non-empty declared variable is exposed
  17. as an environment connection automatically, so common providers usually need no config:
  18. ```bash
  19. export ANTHROPIC_API_KEY="your-key"
  20. ```
  21. For a custom provider, `env` declares the variables that can supply its key:
  22. ```jsonc title="opencode.jsonc"
  23. {
  24. "$schema": "https://opencode.ai/config.json",
  25. "providers": {
  26. "acme": {
  27. "env": ["ACME_API_KEY"]
  28. }
  29. }
  30. }
  31. ```
  32. When several credential sources exist, OpenCode uses the stored credential first, then the first non-empty variable in
  33. `env`, then `settings.apiKey`. Use config substitution instead of committing a literal key:
  34. ```jsonc title="opencode.jsonc"
  35. {
  36. "$schema": "https://opencode.ai/config.json",
  37. "providers": {
  38. "acme": {
  39. "settings": {
  40. "apiKey": "{env:ACME_API_KEY}"
  41. }
  42. }
  43. }
  44. }
  45. ```
  46. <Warning>Do not commit API keys or authorization headers to your repository.</Warning>
  47. ## Configure
  48. The `providers` object is keyed by provider ID. Each provider accepts these fields:
  49. | Field | Purpose |
  50. | --- | --- |
  51. | `name` | Display name. |
  52. | `env` | Ordered environment variable names that provide a connection. |
  53. | `package` | Runtime provider package. |
  54. | `settings` | JSON settings passed to the runtime package, such as `baseURL`. |
  55. | `headers` | String-valued HTTP headers added to requests. |
  56. | `body` | JSON fields merged into request bodies. |
  57. | `models` | Models to add or override, keyed by catalog model ID. |
  58. Configuration files are applied from lowest to highest precedence. `settings` and `body` are deep-merged. Headers are
  59. merged case-insensitively. At request time, provider values are inherited by the model, model values override them, and
  60. the selected variant is applied last.
  61. ### Endpoint
  62. Override `settings.baseURL` to send an existing provider through a proxy or compatible endpoint. Its existing package,
  63. models, and connection continue to apply:
  64. ```jsonc title="opencode.jsonc"
  65. {
  66. "$schema": "https://opencode.ai/config.json",
  67. "providers": {
  68. "anthropic": {
  69. "settings": {
  70. "baseURL": "https://llm-proxy.example.com/anthropic"
  71. }
  72. }
  73. }
  74. }
  75. ```
  76. `settings` is package-specific. A field only has an effect when the selected package supports it.
  77. ### Headers and body
  78. Headers and body fields can be set at provider, model, or variant scope:
  79. ```jsonc title="opencode.jsonc"
  80. {
  81. "$schema": "https://opencode.ai/config.json",
  82. "providers": {
  83. "openai": {
  84. "headers": {
  85. "X-Gateway-Tenant": "engineering"
  86. },
  87. "body": {
  88. "metadata": {
  89. "application": "opencode"
  90. }
  91. },
  92. "models": {
  93. "gpt-5.2": {
  94. "headers": {
  95. "X-Model-Policy": "coding"
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. ```
  103. These are request overlays, not a generic authentication scheme. Prefer `/connect`, `env`, or `settings.apiKey` for
  104. provider credentials unless the endpoint explicitly requires a custom header.
  105. ### Provider packages
  106. For an OpenAI-compatible service, use the V2 native compatible package. The model map is explicit because a custom
  107. provider has no Models.dev catalog entries:
  108. ```jsonc title="opencode.jsonc"
  109. {
  110. "$schema": "https://opencode.ai/config.json",
  111. "model": "acme/qwen3-coder",
  112. "providers": {
  113. "acme": {
  114. "name": "Acme Gateway",
  115. "env": ["ACME_API_KEY"],
  116. "package": "@opencode-ai/llm/providers/openai-compatible",
  117. "settings": {
  118. "baseURL": "https://llm.acme.example/v1"
  119. },
  120. "models": {
  121. "qwen3-coder": {
  122. "name": "Qwen 3 Coder",
  123. "capabilities": {
  124. "tools": true,
  125. "input": ["text"],
  126. "output": ["text"]
  127. },
  128. "limit": {
  129. "context": 131072,
  130. "output": 32768
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. ```
  138. Omit `env` for an endpoint that does not require authentication. The native compatible package requires
  139. `settings.baseURL` and uses bearer authentication when a key is available.
  140. The `package` field supports two runtime contracts:
  141. | Form | Contract |
  142. | --- | --- |
  143. | `"@opencode-ai/llm/providers/openai-compatible"` | A V2 native package exporting `model(modelID, settings)`. An npm specifier or absolute `file://` URL may use the same contract. |
  144. | `"aisdk:@ai-sdk/openai-compatible"` | An AI SDK provider package. The `aisdk:` prefix is required. |
  145. Native packages receive the merged `settings` plus the resolved `apiKey`, `headers`, `body`, and `limits`. AI SDK
  146. packages receive their merged provider options. Use a package's own documentation for accepted settings; OpenCode does
  147. not validate package-specific keys.
  148. `package` may also be set on one model to override the provider package for that model.
  149. ### Models
  150. Add a model under a provider's `models` map. The object key is the model ID used in OpenCode; `modelID` is the ID sent to
  151. the provider:
  152. ```jsonc title="opencode.jsonc"
  153. {
  154. "$schema": "https://opencode.ai/config.json",
  155. "model": "openai/coding",
  156. "providers": {
  157. "openai": {
  158. "models": {
  159. "coding": {
  160. "modelID": "gpt-5.2",
  161. "name": "GPT-5.2 Coding"
  162. }
  163. }
  164. }
  165. }
  166. }
  167. ```
  168. See [Models](/models) for model selection, defaults, capabilities, limits, costs, and variants.