Switch between Anthropic, Gemini and OpenRouter
Run three explicit provider calls with private credential references and inspect input, output and total tokens for each response.
// Switch native Anthropic, Gemini and OpenRouter clients with private SecretRefs.
// Configure the three keys through hidden terminal input before running:
// aner secrets set anthropic
// aner secrets set gemini
// aner secrets set openrouter
// Requires a compatible updated native development runtime.
// Checking sends no requests. Running sends three requests and may incur charges.
// Model identifiers are illustrative; choose models available to your accounts.
// For env/dotenv configuration and token semantics, see the companion notebooks.
import aner.llm
import aner.secrets
// Select exactly "stored", "env" or "dotenv". Never place a private key here.
let key_source = "stored"
let env_file = ".env.local" // Or .env, .env.production, .env.development; absolute paths work.
var anthropic_key = secrets.stored("anthropic")
var gemini_key = secrets.stored("gemini")
var openrouter_key = secrets.stored("openrouter")
if key_source == "dotenv" {
anthropic_key = secrets.dotenv(path: env_file, name: "ANTHROPIC_API_KEY")
gemini_key = secrets.dotenv(path: env_file, name: "GEMINI_API_KEY")
openrouter_key = secrets.dotenv(path: env_file, name: "OPENROUTER_API_KEY")
}
if key_source == "env" {
anthropic_key = secrets.env("ANTHROPIC_API_KEY")
gemini_key = secrets.env("GEMINI_API_KEY")
openrouter_key = secrets.env("OPENROUTER_API_KEY")
}
let output_budget = 1024
let messages = List(
llm.message(role: "user", content: "Explain precisely and concisely in three sentences why preprocessing must be fitted on training data only."))
fn report_tokens(label: String, count: Int64) {
if count < 0 {
print(label + ": unavailable")
} else {
print(label + ": " + String(count))
}
}
// One request. Rerunning this cell sends another request.
{
let model = llm.client(provider: "anthropic", model: "claude-sonnet-5-5",
key: anthropic_key)
let answer = model.chat(messages, max_tokens: output_budget)
print("Provider: " + model.provider())
print("Requested model: " + model.model())
print("Answer:")
print(answer.text())
print("Returned model: " + answer.model())
print("Finish reason: " + answer.finish_reason())
report_tokens("Input tokens", answer.input_tokens())
report_tokens("Output tokens", answer.output_tokens())
report_tokens("Total tokens", answer.total_tokens())
}
// One request. Rerunning this cell sends another request.
{
let model = llm.client(provider: "gemini", model: "gemini-3.7-flash",
key: gemini_key)
let answer = model.chat(messages, max_tokens: output_budget)
print("Provider: " + model.provider())
print("Requested model: " + model.model())
print("Answer:")
print(answer.text())
print("Returned model: " + answer.model())
print("Finish reason: " + answer.finish_reason())
report_tokens("Input tokens", answer.input_tokens())
report_tokens("Output tokens", answer.output_tokens())
report_tokens("Total tokens", answer.total_tokens())
}
// One request. Rerunning this cell sends another request.
{
let model = llm.client(provider: "openrouter", model: "openai/gpt-4.1-mini",
key: openrouter_key)
let answer = model.chat(messages, max_tokens: output_budget)
print("Provider: " + model.provider())
print("Requested model: " + model.model())
print("Answer:")
print(answer.text())
print("Returned model: " + answer.model())
print("Finish reason: " + answer.finish_reason())
report_tokens("Input tokens", answer.input_tokens())
report_tokens("Output tokens", answer.output_tokens())
report_tokens("Total tokens", answer.total_tokens())
}
Run step by step in VS Code.
The provider switching notebook separates private credential references, shared request settings, and three explicit request cells for Anthropic, Google Gemini and OpenRouter. It opens in Production mode with empty outputs and Shared variables enabled.
- Install a compatible current native Aner development runtime with all three adapters and matching notebook integration using the editor guide.
- Configure the three stored labels through hidden terminal input:
aner secrets set anthropic,aner secrets set geminiandaner secrets set openrouter. Keep key values outside notebook cells and source. The credential guide explainssecrets.envandsecrets.dotenvalternatives. A dotenv reference selects exactly one private.env,.env.production,.env.developmentor.env.localfile, with no discovery or merging. - Download the
.anernbnotebook and open it in your VS Code project. If it appears as JSON, choose Reopen Editor With… → Aner Notebook. Select theAner — notebookkernel and keep Shared variables active. - Review the available provider model identifiers, shared prompt, ordered history and maximum output budget of 1024. Client construction sends no request. Run the setup and request cells in order when ready; each request cell calls one provider and may incur charges. Each completed call prints generated text, requested and returned model, finish reason and labeled input, output and total tokens. A count of
-1means unavailable; a reported0remains zero. Totals are per response and are not prices or directly comparable tokenizer measurements.
After editing declarations or earlier settings, use Aner: Restart Notebook Session, then rerun the cells in order. Each repeated request cell sends another provider call. Saved outputs are historical; reopening the file starts without live variables. The notebook guide explains session state and runtime controls.
Run step by step in Jupyter.
Download the .ipynb notebook and open it in a notebook application with the Aner kernel configured. Select Aner and run its cells from top to bottom. The Jupyter guide explains kernel requirements, shared session state and output policies.
The provider switching notebook starts in Production mode with empty outputs. Review all three private credential references, available model identifiers and the shared request settings before running. The three explicit request cells each send one provider call. Read input, output and total tokens below each completed cell: -1 means unavailable and a reported 0 remains zero. Restart the kernel after editing declarations and run cells in order. Each rerun may incur provider charges.
Make it your experiment.
With a compatible development runtime installed, save this program as examples/llm_provider_switching.aner inside a folder for your experiment. Open a terminal in that folder, then check and run the program.
aner check examples/llm_provider_switching.aner
aner run examples/llm_provider_switching.anerThe aner command must be on your PATH. Follow the installation guide if your terminal cannot find it.
Use a compatible current native Aner development runtime with the Anthropic, Google Gemini and OpenRouter adapters, secrets.dotenv and expanded token usage accessors. Notebook execution requires a matching Aner VS Code extension or Jupyter kernel integration. Checking sends no inference request and resolves no credential. Running the source or Run All in either notebook sends three explicit requests and can incur provider charges. Configure an available model and an opaque credential reference for each provider.
aner secrets set anthropic
aner secrets set gemini
aner secrets set openrouter
aner secrets status anthropic
aner secrets status gemini
aner secrets status openrouterThe companion source and each notebook send three explicit requests using the same prompt, ordered history and maximum output budget of 1024. Each response reports its own input, output and total tokens. Provider tokenizers differ, and these counts do not establish comparative model quality or charges. Client construction and syntax checking send no request. Each repeated request cell sends another call and may incur provider charges.
Read token accounting and provider requirements