Skip to main content

BAI code Introduction

BAI code mascot

BAI code is a command-line AI programming tool that supports multi-model switching, session management, and context compression.

Supported Platforms

OSArchitecture
macOSx86_64, arm64 (Apple Silicon)
Linuxx86_64
Windowsx86_64 (AMD64)

Prerequisites

  • Python 3.10 – 3.13
  • macOS / Linux: curl is required
  • Windows: PowerShell 5.1+ is required

Installation

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.

Configuration

BAI code is configured via environment variables and supports automatic loading from a .env file (searching upward to $HOME).

Environment Variables

VariableDefaultRequiredDescription
BAI_API_KEYYesAPI key (used for any service compatible with the OpenAI protocol)
BAI_MODELclaude-sonnet-4.6NoModel name to use
BAI_BASE_URLhttps://api.b.ai/v1NoCustom API Base URL (used when connecting to a third-party service; defaults to OpenAI endpoint if omitted)
BAICODE_MAX_TOKENS4096NoMaximum tokens to generate per request
BAICODE_TEMPERATURE0NoSampling temperature; 0 for deterministic output
BAICODE_MAX_CONTEXT128000NoContext 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:

PathContentDescription
~/.baicode/sessions/session_<timestamp>.jsonSaved session files, each containing full conversation history and the model name used
~/.baicode_historyPlain textREPL 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": [...]
}
danger

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

OptionDescription
-m, --modelModel name (default: $BAICODE_MODEL or gpt-4o)
--base-urlAPI Base URL (default: $BAI_BASE_URL)
--api-keyAPI Key (default: $BAI_API_KEY)
-p, --promptOne-time prompt; exits after running in non-interactive mode
-r, --resumeResume a saved session by ID
-v, --versionShow version number

Built-in Commands

CommandDescription
/helpShow help
/resetClear conversation history
/modelShow current model
/model <name>Switch model
/tokensShow token usage and estimated cost
/compactManually compress context
/diffView files modified in this session
/saveSave session to disk
/sessionsList all saved sessions
quit / exitExit

Input Shortcuts

ShortcutDescription
EnterSubmit message
Esc + EnterInsert newline (useful for pasting multi-line code)
Ctrl+CInterrupt current generation