BAI code Introduction
BAI code is a command-line AI programming tool that supports multi-model switching, session management, and context compression.
Supported Platforms
| OS | Architecture |
|---|---|
| macOS | x86_64, arm64 (Apple Silicon) |
| Linux | x86_64 |
| Windows | x86_64 (AMD64) |
Prerequisites
- Python 3.10 – 3.13
- macOS / Linux:
curlis required - Windows: PowerShell 5.1+ is required
Installation
- macOS / Linux
- Windows
Run the following command in your terminal:
# Download the install script
curl -fsSL https://raw.githubusercontent.com/BAI-labs/BAI-tools/refs/heads/main/scripts/baicode_install.sh -o baicode_install.sh
# Run the install script
bash baicode_install.sh
The installer will ask whether to create a virtual environment (.venv). Enter y to confirm, or press Enter to skip.
Run the following command in PowerShell:
# Download the install script
iwr -useb https://raw.githubusercontent.com/BAI-labs/BAI-tools/refs/heads/main/scripts/baicode_install.ps1 -OutFile baicode_install.ps1
# Run the install script
.\baicode_install.ps1
The installer will ask whether to create a virtual environment (.venv). Enter y to confirm, or press Enter to skip.
If script execution is blocked by the execution policy, run the following command first, then retry:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
Configuration
BAI code is configured via environment variables and supports automatic loading from a .env file (searching upward to $HOME).
Environment Variables
| Variable | Default | Required | Description |
|---|---|---|---|
BAI_API_KEY | — | Yes | API key (used for any service compatible with the OpenAI protocol) |
BAI_MODEL | claude-sonnet-4.6 | No | Model name to use |
BAI_BASE_URL | https://api.b.ai/v1 | No | Custom API Base URL (used when connecting to a third-party service; defaults to OpenAI endpoint if omitted) |
BAICODE_MAX_TOKENS | 4096 | No | Maximum tokens to generate per request |
BAICODE_TEMPERATURE | 0 | No | Sampling temperature; 0 for deterministic output |
BAICODE_MAX_CONTEXT | 128000 | No | Context window size in tokens; automatically compressed when exceeded |
Creating a .env File
Create a .env in the project root (automatically loaded on startup without overriding existing system environment variables):
BAI_API_KEY=sk-...
BAI_MODEL=claude-sonnet-4.6
# BAI_BASE_URL=https://api.b.ai/v1
# BAICODE_MAX_TOKENS=4096
# BAICODE_TEMPERATURE=0
# BAICODE_MAX_CONTEXT=128000
Data Directory
BAI code writes the following files under the user's home directory. No manual creation is required — they are automatically generated on first use:
| Path | Content | Description |
|---|---|---|
~/.baicode/sessions/ | session_<timestamp>.json | Saved session files, each containing full conversation history and the model name used |
~/.baicode_history | Plain text | REPL input history, supports up/down arrow navigation |
Session file format:
{
"id": "session_1712345678",
"model": "claude-sonnet-4.6",
"saved_at": "2026-04-10 12:00:00",
"messages": [...]
}
The data directory is managed automatically by the program. Do not manually modify the JSON files inside, as this may cause session recovery to fail.
Cleaning Data
# Delete all sessions
rm -rf ~/.baicode/sessions/
# Clear REPL history
rm ~/.baicode_history
CLI Reference
Basic Usage
baicode [-h] [-m MODEL] [--base-url URL] [--api-key KEY] [-p PROMPT] [-r ID] [-v]
Options
| Option | Description |
|---|---|
-m, --model | Model name (default: $BAICODE_MODEL or gpt-4o) |
--base-url | API Base URL (default: $BAI_BASE_URL) |
--api-key | API Key (default: $BAI_API_KEY) |
-p, --prompt | One-time prompt; exits after running in non-interactive mode |
-r, --resume | Resume a saved session by ID |
-v, --version | Show version number |
Built-in Commands
| Command | Description |
|---|---|
/help | Show help |
/reset | Clear conversation history |
/model | Show current model |
/model <name> | Switch model |
/tokens | Show token usage and estimated cost |
/compact | Manually compress context |
/diff | View files modified in this session |
/save | Save session to disk |
/sessions | List all saved sessions |
quit / exit | Exit |
Input Shortcuts
| Shortcut | Description |
|---|---|
Enter | Submit message |
Esc + Enter | Insert newline (useful for pasting multi-line code) |
Ctrl+C | Interrupt current generation |