Skip to main content
Agent Mode transforms ChatCLI from a passive assistant into a proactive executor. Delegate a complete task, and the AI creates, presents, and β€” with your approval β€” executes an action plan.

How to Start

Use /agent or /run, followed by your task in natural language:
The AI will respond with an Action Plan: a list of structured commands for review.

The Agent Cycle


How the Agent Works Internally

At the heart of agent mode is a ReAct loop (Reason + Act) implemented in the processAIResponseAndAct() function. This loop allows the AI to reason about the problem, execute actions, and use the results to decide the next steps.

The ReAct Loop

Each agent turn follows this sequence:
  1. Build history β€” The conversation history is assembled with an anchor reminder (format reminder) appended at the end. This anchor reinforces the expected response format for the AI, preventing it from β€œforgetting” instructions during long conversations.
  2. Call the LLM β€” The complete history is sent to the configured AI provider.
  3. Parse the response β€” The response is analyzed to extract reasoning, explanations, tool calls, and commands.
  4. Execute actions β€” Tools and commands are executed with appropriate security controls.
  5. Inject feedback β€” Execution results are injected back into the history as context messages.
  6. Next turn β€” The loop returns to step 1, until the AI completes the task or the turn limit is reached.
The maximum number of turns is configurable (default: 50, maximum: 200). When the limit is reached, the agent terminates the loop and displays the last state to the user.

System Prompt Composition

The agent’s system prompt is dynamically assembled from several sources:
  • Workspace context β€” Bootstrap files like SOUL.md, USER.md, and persistent memory
  • Tool descriptions β€” Complete list of available plugins and their parameters
  • Active persona β€” If a custom persona is configured, it is included
  • Format instructions β€” The AgentFormatInstructions that define the expected response format
The /agent, /coder, and /run modes share the same ReAct loop. The difference lies only in the prompt instructions β€” /coder uses CoderSystemPrompt which emphasizes code editing, while /agent uses instructions geared toward general task execution.

AI Response Format

The LLM response is parsed to identify multiple structured elements. Each block type has different behavior in the UI:
The parser is stateful (not regex-based) to correctly handle XML tags containing quoted attributes with special characters.

Cancellation and Ctrl+C

The agent supports graceful cancellation at any point during execution:
  • During LLM call β€” Ctrl+C cancels the request via context.WithCancel(). Any partial response received up to that point is discarded.
  • Per-turn check β€” At the start of each ReAct loop turn, the agent checks context.Done(). If the context has been cancelled, the loop terminates immediately.
  • Signal handling β€” SIGINT and SIGTERM are caught by the runWithCancellation() function, which coordinates graceful shutdown.
  • Type-ahead queue β€” Messages typed by the user during AI processing are queued and processed once the current turn finishes. This prevents input loss. Works in both /agent AND /coder. The spinner shows (N queued) in real time as you press Enter β€” no need to wait for the turn to close to see the counter rise.
If the AI gets β€œstuck” in a long loop, press Ctrl+C once to cancel the current turn. You can then rephrase your instruction.

History and Compaction

The agent automatically manages conversation history size to avoid exceeding the model’s token limit.

Compaction Strategy

When the history exceeds 60% of the model’s token budget, compaction is triggered in 3 progressive levels:
  1. Trimming β€” Injected context messages (tool results, command outputs) longer than 3000 characters are truncated, preserving the beginning and end.
  2. Summarization β€” Intermediate messages are summarized by the AI itself, keeping key points.
  3. Emergency truncation β€” If the previous levels are not sufficient, the oldest messages are removed.
Across all levels, the 8 most recent messages are always preserved to maintain immediate context.

Checkpoint and /rewind

At the start of each agent interaction, a checkpoint of the history is saved. This allows using /rewind (or Esc+Esc) to return to the exact state before the agent’s last action.

Action Plan Interface

After planning, you will see a dedicated screen with two views (toggle with p):
Ideal for an overview of the flow, showing status and the first line of each command.

Interactive Menu

The menu allows you to manage execution with precision:
Use tN (test) to verify what a command will do. If it looks good, execute with N. If something goes wrong, use cN to ask the AI to fix the plan.

Security

Dangerous commands (rm -rf, sudo, mkfs, dd) are blocked by default. ChatCLI will require explicit confirmation before allowing their execution.
You always have the final say. No command is executed without your approval.

Agent Mode Security

Agent mode implements multiple layers of protection to ensure safe command execution.

Command Allowlist (CHATCLI_AGENT_SECURITY_MODE)

ChatCLI uses a command allowlist with over 150 pre-approved commands, organized into 8 categories:
Only commands in the allowlist can be executed. Any command outside the list is automatically blocked.
To add custom commands to the allowlist:
If a legitimate command is blocked in strict mode, add it via CHATCLI_AGENT_ALLOWLIST instead of switching to permissive mode.
Example: blocked command and how to allow it

Sensitive Read Path Protection (CHATCLI_AGENT_WORKSPACE_STRICT)

When enabled, the agent can only read files within the current workspace. Sensitive paths are always blocked:

Shell Config (CHATCLI_AGENT_SOURCE_SHELL_CONFIG)

Breaking change: Sourcing shell configuration files (~/.bashrc, ~/.zshrc) is now opt-in. In previous versions, sourcing was implicit. If your commands depend on shell aliases or functions, enable this explicitly.
When enabled, ChatCLI validates:
  • File ownership β€” The file must be owned by the current user
  • File size β€” A safety limit to prevent sourcing excessively large files

Command Output Sanitization

ChatCLI protects against prompt injection in command output:
  • Prompt injection detection β€” Suspicious patterns in output (e.g., instructions for the AI to ignore rules) are detected and sanitized before being injected into context
  • Size limits β€” Command outputs are truncated to prevent excessive token consumption
For a complete overview of all security measures, see the Security and Hardening documentation.

Unified History and Context

Agent mode shares the same conversation history as chat and coder. This means you can:
  • Start a conversation in chat, enter /agent, and the AI will have all the previous context
  • Use /compact to reduce history when it gets large
  • Use /rewind (or Esc+Esc) to go back to an earlier point in the conversation
Additionally, the agent automatically receives workspace context (bootstrap files like SOUL.md, USER.md, and persistent memory) in its system prompt.

Agent Mode Configuration

Agent behavior can be tuned via environment variables:

One-Shot Mode (-p flag)

One-shot mode enables non-interactive single-instruction execution, ideal for scripts and automation:

How it works

  1. The instruction is sent to the LLM as a single turn (no ReAct loop).
  2. A thinking animation is displayed while the AI processes.
  3. If the --auto-execute flag is active, the first command block in the response is executed automatically β€” but only after passing the dangerous command check.
  4. The result is displayed and the process exits.
One-shot with --auto-execute does not prompt for confirmation on safe commands. Make sure you trust the instruction before using this combination in scripts.

Workspace and large payloads

Every /agent session receives an isolated scratch directory (exposed via CHATCLI_AGENT_TMPDIR) where the AI can write temporary scripts and read overflow files from the tool-result budget β€” without having to touch the project tree. When a tool result is truncated, the path to the full file is included in the preview and the AI can open it with read_file. For analyses over large payloads (Prometheus /metrics, verbose logs), prefer the delegate_subagent tool: it runs in an isolated context window and returns only the final summary. Details in Session Workspace and Subagent Delegation.

Next Steps

Coder Mode

AI that reads, edits, and tests code in an automated loop.

Session Workspace

Isolated scratch dir for temporary scripts and reading overflows.

Subagent Delegation

Delegate heavy analyses with isolated context.

Conversation Control

Use /compact and /rewind to manage history.

Session Management

Save and reuse your work across projects.