10 Essential Commands for Command-Line AI Coding Assistants

    This guide introduces ten essential commands for using command-line AI coding assistants effectively. It covers model selection, context management, session recovery, project initialization, and non-interactive usage, helping developers avoid common pitfalls and improve workflow efficiency.

    1. Why Master Commands First?

    When developers first use a command-line AI coding assistant, they typically do two things: launch the tool and treat it like a chat window. While this works, it is inefficient for three main reasons.

    First, longer conversations lead to bloated context. Irrelevant early discussions dilute the model’s attention to the current task, causing responses to drift from the core issue.

    Second, the assistant lacks awareness of the project structure. Unless explicitly prompted to read project information, it only relies on the snippets you provide, making its suggestions prone to disconnecting from your actual codebase.

    Third, differences between models and parameters are often overlooked. Different models focus on reasoning capability, response speed, and cost. Choosing the wrong model either wastes money or performs poorly on complex tasks.

    The following ten commands address these issues. Each is an executable command with practical usage notes.

    2. Environment Preparation

    The official command-line tool requires Node.js 18 or higher. After installation, verify the version with:

    1. claude --version

    Subsequent operations should be performed in the project’s root directory. The tool treats the current directory as the workspace, scoping all file reads, writes, and code searches accordingly. If launched from a home directory or an unrelated folder, it will encounter cluttered content.

    Note that installation methods and minimum version requirements may change with official updates. Always refer to the official documentation if issues arise.

    3. Startup and Model Selection

    1. claude: Enter Interactive Session

    1. claude

    This is the most basic entry point. It launches the interactive interface where all subsequent queries, code modifications, and command executions take place.

    2. claude --model sonnet: Specify Model at Startup

    1. claude --model sonnet

    The --model flag specifies the model for the current session. There are three common aliases corresponding to typical tiers:

    Alias General Positioning Applicable Scenarios
    sonnet Balanced, daily development default Code writing, minor refactoring, general Q&A
    opus Strong reasoning, higher capability, higher cost Complex architecture design, tricky bug analysis
    haiku Lightweight, fast response, low cost Simple Q&A, batch small tasks

    The specific model versions these aliases point to may change with official updates. Beginners are advised to default to sonnet and switch only for complex problems to avoid unnecessary costs for simple tasks.

    3. /model: Switch Model Mid-Session

    1. /model

    Entering /model in the interactive interface opens the model selection list. Switching models does not clear the current conversation. For instance, if a feature discussion is halfway through and hits a complex concurrency issue, you can temporarily switch to a more powerful model and switch back once the issue is resolved.

    4. Session Recovery

    4. claude --resume: Resume Historical Session

    1. claude --resume

    This command lists historical sessions; select one to continue. To continue only the most recent session, use claude --continue or its shorthand claude -c.

    Note that this recovers the chat history, not the code state. The model can see previous requirements, constraints, and conclusions, but file changes on disk are not rolled back or reapplied. After resuming, verify the current state of your files before continuing development.

    5. Context Management

    5. /clear: Clear Conversation

    1. /clear

    After clearing, the model no longer carries previous conversation context, equivalent to a fresh session. This is ideal when the topic completely changes, such as moving from frontend styling adjustments to database index optimization.

    Be sure to note down any conclusions you need to keep before clearing. Once /clear is executed, the historical conversation cannot be retrieved in the current session.

    6. /compact: Compress Conversation

    1. /compact

    /compact organizes lengthy conversations into summaries, freeing up context space while retaining key information. It accepts supplementary instructions to specify the summary’s focus:

    1. /compact Keep only conclusions and to-do items related to database migration

    The rule of thumb is simple: use /compact when a single task’s conversation gets too long, and use /clear when the task is finished or the topic has changed.

    6. Project Initialization and Status Check

    7. /init: Generate Project Description

    1. /init

    /init instructs the assistant to analyze the project structure and code organization, generating a project description file for subsequent sessions to read. When onboarding to a new project, this saves repetitive code pasting and ensures generated code suggestions better match the existing project style.

    Three prerequisites apply. First, execute it in the project root directory. Second, ensure read and write permissions for the directory. Third, manually review the generated file. The assistant’s analysis might miss modules or misinterpret conventions; incorrect entries must be corrected promptly, otherwise future sessions will treat the errors as facts.

    8. /context: View Context Usage

    1. /context

    This command displays the current context usage. Newer versions support this command; if your version does not, use /status to view basic information like the model, version, and account.

    When usage approaches the limit, response quality often degrades. If you see usage continuously rising, consider executing /compact or /clear.

    9. /permissions: View and Adjust Tool Permissions

    1. /permissions

    Before performing file modifications, running commands, and other operations, the assistant is constrained by permission rules. /permissions allows you to view current rules and adjust which operations require confirmation and which can execute directly.

    Confirm the scope of impact before adjusting permissions. Loosening permissions reduces confirmation steps but also allows the assistant to make more changes without your notice. Beginners are advised to retain the default confirmation mechanism and gradually loosen it once familiar with the workflow.

    7. Non-Interactive Invocation

    10. claude -p: Single Prompt Output

    1. claude -p "Explain the purpose of the formatDate function in src/utils/date.ts"

    The -p flag indicates non-interactive mode. The assistant outputs the result and exits directly without entering the chat interface. It is suitable for scripts, pipelines, or asking a quick standalone question.

    Pay special attention to permissions in non-interactive mode. Without someone present to confirm, the assistant may not be able to prompt for authorization, so you should clarify permission settings beforehand to prevent unauthorized file modifications. The output format can be adjusted with the --output-format parameter; refer to claude --help for specific options.

    1. Navigate to the project root directory, verify the environment with claude --version, and then run /init to establish the project description.
    2. Default to sonnet, temporarily switching with /model for complex issues.
    3. Execute /clear when switching topics, /compact when a single task gets too long, and periodically check usage with /context.
    4. Use claude --resume to recover after an interruption, verifying file status once resumed.
    5. Use /permissions to confirm the scope before modifying permission settings.
    6. Exit the session with /exit, and run claude --help if unsure about a specific parameter.

    These ten commands cover the complete workflow from startup to conclusion. Once proficient, you will better understand which command solves which type of problem, and feel more confident deciding when to clear, when to compact, and when to switch models.