# How to Switch Between Provider Accounts Programmatically in jcode

> Learn to switch provider accounts programmatically in jcode using switch_command. Update and persist your active account through the CLI processor with ease.

- Repository: [Jeremy Huang/jcode](https://github.com/1jehuang/jcode)
- Tags: how-to-guide
- Published: 2026-04-30

---

**To switch between provider accounts programmatically in jcode, use the `switch_command` method on the `MultiAccountProviderKind` enum to generate a `/provider switch {Provider} {label}` string, then execute it through the CLI processor to update the active account and persist the change to `~/.jcode/config.toml`.**

The jcode CLI tool supports managing multiple API keys for the same AI provider, enabling seamless transitions between work and personal accounts without manual reconfiguration. Understanding how to switch between provider accounts programmatically in jcode allows you to automate billing segregation in batch scripts, CI pipelines, and custom integrations.

## Understanding the Account Switching Architecture

The core of the account switching logic resides in **[`src/usage/model.rs`](https://github.com/1jehuang/jcode/blob/main/src/usage/model.rs)**. This file defines the **`MultiAccountProviderKind`** enum (lines 253-258), which enumerates all supported multi-account providers such as OpenAI and Anthropic.

Each variant implements the **`switch_command(&self, label: &str) -> String`** method starting at line 266. This method constructs the exact CLI command string required to activate a specific account by its user-defined label.

## Generating the Switch Command

The `switch_command` method builds a command in the format `/provider switch {Provider} {label}`. When invoked, it returns a string that mimics user input from the TUI command box.

```rust
use jcode::usage::model::MultiAccountProviderKind;

// Generate the switch command for a specific account
let provider = MultiAccountProviderKind::OpenAI;
let cmd = provider.switch_command("work-account");

// cmd now contains: "/provider switch OpenAI work-account"

```

## Executing the Switch Programmatically

According to the jcode source code, you execute the generated command by passing it to the internal command processor. The entry point `jcode::cli::run` or similar processing routines parses the string and triggers the provider switching flow.

```rust
use jcode::usage::model::MultiAccountProviderKind;

// 1. Select the provider kind
let provider = MultiAccountProviderKind::OpenAI;

// 2. Specify the account label configured during account setup
let account_label = "personal";

// 3. Build the switch command
let switch = provider.switch_command(account_label);
assert_eq!(switch, "/provider switch OpenAI personal");

// 4. Execute via the CLI processor (pseudo-API for illustration)
jcode::cli::process_command(&switch);

```

In production code, use **`jcode::cli::run`** or **`run_with_initial_command`** as the actual entry point to process the command string.

## Configuration Persistence and UI Updates

When the switch command executes, jcode triggers a multi-step internal workflow across several source files:

- **[`src/cli/provider_init.rs`](https://github.com/1jehuang/jcode/blob/main/src/cli/provider_init.rs)**: Parses the `/provider switch` instruction and validates the command syntax.
- **[`src/config/config_file.rs`](https://github.com/1jehuang/jcode/blob/main/src/config/config_file.rs)**: Persists the newly selected active account to `~/.jcode/config.toml`, ensuring the change survives across sessions.
- **[`src/usage/display.rs`](https://github.com/1jehuang/jcode/blob/main/src/usage/display.rs)**: Updates the TUI to show the active account marker (✦) next to the selected provider.
- **[`src/usage/provider_fetch.rs`](https://github.com/1jehuang/jcode/blob/main/src/usage/provider_fetch.rs)**: Refreshes the usage cache immediately so subsequent `/usage` calls reflect data from the newly activated account.

The process locates the matching `ProviderAccount` by label, marks it as active, updates the cached state, and refreshes the UI without requiring a restart.

## Summary

- The **`MultiAccountProviderKind`** enum in [`src/usage/model.rs`](https://github.com/1jehuang/jcode/blob/main/src/usage/model.rs) defines supported providers and implements the **`switch_command`** method to generate CLI strings.
- Call `switch_command("label")` to create a `/provider switch {Provider} {label}` command string programmatically.
- Pass the generated string to **`jcode::cli::run`** or the internal command processor to execute the switch.
- The switch updates `~/.jcode/config.toml` immediately and refreshes the usage cache via [`src/usage/provider_fetch.rs`](https://github.com/1jehuang/jcode/blob/main/src/usage/provider_fetch.rs).

## Frequently Asked Questions

### How do I list available account labels before switching?

Query the configuration or usage display modules to retrieve registered accounts. The **[`src/usage/display.rs`](https://github.com/1jehuang/jcode/blob/main/src/usage/display.rs)** file renders account lists with active markers, while **[`src/config/config_file.rs`](https://github.com/1jehuang/jcode/blob/main/src/config/config_file.rs)** stores the account definitions including their user-defined labels.

### Can I switch accounts without launching the full TUI?

Yes. Use **`jcode::cli::run_with_initial_command`** and pass the switch command string as the initial command. This executes the switch logic immediately and can exit or proceed based on your implementation, enabling headless account switching in scripts and automation tools.

### Where does jcode store the active account state?

jcode persists the active account selection in **`~/.jcode/config.toml`**, managed by **[`src/config/config_file.rs`](https://github.com/1jehuang/jcode/blob/main/src/config/config_file.rs)**. This ensures that your selected account remains active across terminal sessions and jcode restarts without requiring repeated switch calls.

### What providers support multi-account switching?

The **`MultiAccountProviderKind`** enum in [`src/usage/model.rs`](https://github.com/1jehuang/jcode/blob/main/src/usage/model.rs) (lines 253-258) defines the supported providers, which currently include **OpenAI** and **Anthropic**. Each variant implements the same `switch_command` interface, allowing consistent programmatic control across different AI services.