How to Start
Use/agent or /run, followed by your task in natural language:
The Agent Cycle
How the Agent Works Internally
At the heart of agent mode is a ReAct loop (Reason + Act) implemented in theprocessAIResponseAndAct() 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:- 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.
- Call the LLM — The complete history is sent to the configured AI provider.
- Parse the response — The response is analyzed to extract reasoning, explanations, tool calls, and commands.
- Execute actions — Tools and commands are executed with appropriate security controls.
- Inject feedback — Execution results are injected back into the history as context messages.
- Next turn — The loop returns to step 1, until the AI completes the task or the turn limit is reached.
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
AgentFormatInstructionsthat 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:| Element | Tag/Format | UI Behavior |
|---|---|---|
| Reasoning | <reasoning>...</reasoning> | Displayed as a “brain” icon card — AI’s internal thinking |
| Explanation | <explanation>...</explanation> | Displayed as a pinned card — user-facing explanation |
| Final summary | <final_summary>...</final_summary> | Task completion summary |
| Tool call | <tool_call>...</tool_call> | Plugin invocation (file_edit, web_search, etc.) |
| Multi-agent | <agent_call>...</agent_call> | Dispatches sub-tasks to parallel agents (3+ independent tasks) |
| Shell command | ```execute:shell or ```bash | Command block for execution (legacy format) |
| Plain text | Text without tags | Displayed as normal chat response |
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+Ccancels the request viacontext.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 —
SIGINTandSIGTERMare caught by therunWithCancellation()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.
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:- Trimming — Injected context messages (tool results, command outputs) longer than 3000 characters are truncated, preserving the beginning and end.
- Summarization — Intermediate messages are summarized by the AI itself, keeping key points.
- Emergency truncation — If the previous levels are not sufficient, the oldest messages are removed.
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 withp):
- Compact View (Default)
- Full View
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:| Action | Description |
|---|---|
[N] | Execute Command N — runs a single step of the plan (e.g., 1) |
a | Execute All — runs all pending commands in sequence |
eN | Edit Command N — opens the command in an editor for modification |
tN | Test (Dry-Run) — simulates execution without making changes |
cN | Continue from N — sends the output to the AI and asks for next steps |
pcN | Pre-Execution Context — adds information for the AI to refine the command |
acN | Post-Execution Context — sends the output with new context |
vN | View Output — opens the full output in a pager (less) |
wN | Save Output — saves the command output to a temporary file |
p | Toggle Plan — switches between compact and full view |
r | Redraw Screen — clears the screen |
q | Quit — exits Agent Mode and returns to chat |
Security
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:| Category | Examples |
|---|---|
| File operations | ls, cp, mv, mkdir, find, stat |
| Text processing | grep, sed, awk, sort, cut, jq |
| Development | git, go, npm, python, make, cargo |
| Containers | docker, kubectl, helm, podman |
| Network | curl, wget, ping, dig, nslookup |
| System info | ps, df, du, uname, whoami |
| Editors | vim, nano, code, emacs |
| Shell | echo, cat, head, tail, wc, tee |
- strict (default)
- permissive
Only commands in the allowlist can be executed. Any command outside the list is automatically blocked.
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:| Path | Reason |
|---|---|
~/.ssh/ | SSH private keys |
~/.aws/ | AWS credentials |
~/.gcloud/ | Google Cloud credentials |
~/.kube/config | Kubernetes credentials |
/etc/shadow | System passwords |
*.pem, *.key | Certificates and private keys |
Shell Config (CHATCLI_AGENT_SOURCE_SHELL_CONFIG)
- 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
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
/compactto reduce history when it gets large - Use
/rewind(or Esc+Esc) to go back to an earlier point in the conversation
Agent Mode Configuration
Agent behavior can be tuned via environment variables:| Variable | Default | Description |
|---|---|---|
CHATCLI_AGENT_PLUGIN_MAX_TURNS | 50 | Maximum number of ReAct loop turns. Hard maximum: 200. |
CHATCLI_AGENT_CMD_TIMEOUT | 10m | Timeout for shell command execution. Maximum: 1 hour. |
CHATCLI_AGENT_PLUGIN_TIMEOUT | 15m | Timeout for plugin execution (file_edit, web_search, etc.). |
CHATCLI_AGENT_DENYLIST | (empty) | Regex patterns separated by ; to block commands. E.g., rm\s+-rf;curl.*|.*sh |
CHATCLI_AGENT_ALLOW_SUDO | false | If true, allows execution of commands with sudo. |
CHATCLI_AGENT_PARALLEL_MODE | true | Enables multi-agent dispatch via <agent_call> for parallel tasks. |
CHATCLI_AGENT_MAX_WORKERS | 4 | Max concurrent goroutines for the multi-agent dispatcher. |
CHATCLI_AGENT_WORKER_MAX_TURNS | 10 | Max turns per individual worker. |
CHATCLI_AGENT_WORKER_TIMEOUT | 5m | Per-worker timeout. |
CHATCLI_AGENT_<NAME>_MODEL | (empty) | Model override for a specific built-in agent (e.g., CHATCLI_AGENT_PLANNER_MODEL=claude-opus-4-6). See Multi-Agent. |
CHATCLI_AGENT_<NAME>_EFFORT | (empty) | Effort override for a specific built-in agent (e.g., CHATCLI_AGENT_FORMATTER_EFFORT=low). |
One-Shot Mode (-p flag)
One-shot mode enables non-interactive single-instruction execution, ideal for scripts and automation:How it works
- The instruction is sent to the LLM as a single turn (no ReAct loop).
- A thinking animation is displayed while the AI processes.
- If the
--auto-executeflag is active, the first command block in the response is executed automatically — but only after passing the dangerous command check. - The result is displayed and the process exits.
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.