OpenCode
OpenCode connects to MAG as an OpenAI-compatible provider.
Coding agents make many model calls with long contexts. Usage can be expensive and counts against the allocation of your project. Frontier models cost more per token. To reduce cost, use a smaller model for routine tasks and set a lower context limit. Check your spend on the MAG Dashboard.
1. Set the PAT variable
Make sure the environment variable AMSC_TOKEN contains your PAT. See Step 4.
2. Add the provider
Create the file opencode.json in your project directory. To use MAG in all projects, create ~/.config/opencode/opencode.json instead.
{
"$schema": "https://opencode.ai/config.json",
"compaction": { "auto": true, "prune": true, "reserved": 20000 },
"provider": {
"amsc-mag": {
"npm": "@ai-sdk/openai-compatible",
"name": "AmSC MAG",
"options": {
"baseURL": "https://mag.genesis.americansciencecloud.org/v1",
"apiKey": "{env:AMSC_TOKEN}"
},
"models": {
"claude-haiku": {
"name": "Claude Haiku",
"limit": { "context": 100000, "output": 32000 }
},
"claude-sonnet": {
"name": "Claude Sonnet",
"limit": { "context": 200000, "output": 64000 }
},
"gpt-6-luna": {
"name": "GPT-6 Luna",
"limit": { "context": 272000, "output": 32000 },
"options": { "reasoningEffort": "high" }
},
"gemini-flash": {
"name": "Gemini Flash",
"limit": { "context": 1048576, "output": 65536 },
"options": { "reasoningEffort": "medium" }
}
}
}
}
}
The value {env:AMSC_TOKEN} reads the PAT from the variable. Do not write the PAT in this file.
Some OpenCode versions do not substitute {env:AMSC_TOKEN} in the apiKey of a custom provider (OpenCode issue #19946). Requests then fail with an Unauthorized error, even when the same PAT works in other tools such as Codex. As a workaround, save the PAT to a file that only you can read, and use {file:...} instead:
"apiKey": "{file:~/.config/opencode/amsc-token}"
These settings control context and cost:
limit.contextis the context size OpenCode uses for the model. OpenCode compacts the conversation before this size is reached. The values above are lower than the model maximum on purpose. Prices for Claude Haiku increase above 100,000 tokens. Sonnet and Opus can use up to 1,000,000 tokens, but prices are higher for long contexts. A limit of 200,000 is optional for these models.limit.outputis the maximum number of output tokens for one response.compaction.reservedis the number of tokens that OpenCode keeps free when it compacts.autoturns automatic compaction on.pruneremoves old tool output.options.reasoningEffortsets the reasoning effort. Uselow,medium,high, or a value that the model accepts. See Model Selection & Options.
3. Select a model
-
Start OpenCode in your project directory:
cd my-project
opencode -
Enter the command
/models. -
Select a model from the AmSC MAG provider.
To add a model, add an entry to models. The key is the model name. To find model names, see Using the MAG Dashboard. For advice, see Model Recommendations.
Select a provider
To select the provider that serves the models, add a header to options:
"headers": { "X-AmSC-MAG-Provider": "amazon" }
or:
"headers": { "X-AmSC-MAG-Provider": "google" }
Claude models are available on amazon and google. The Claude web search tool is supported on google and not supported on amazon.