AI-Commander: a ralph-loop AI agent giving LLMs shell access via function calling (TUI + CLI)
pip install aic-agent==1.0.3 Copy PIP instructions
PyPI package: aic-agent — install with pip install aic-agent (the command is aic).
AI-Commander is a ralph-loop AI agent that provides shell access and task planning capabilities to Large Language Models through function calling. It runs a tight agent loop: it plans, executes shell commands, observes the results, and iterates until the requested task is complete — all while you stay in control through an approval gate.
pip install aic-agent This installs the aic command plus every runtime dependency, including the Textual TUI and the context-compressor-llm context compressor, so both the TUI and LLM-based compression work out of the box. (py-landlock is installed only on Linux; elsewhere the agent simply runs unsandboxed.)
pip install --upgrade aic-agent From a checkout of this repository git clone https://github.com/ortegaalfredo/AICommander cd AICommander python3 -m venv venv source venv/bin/activate pip install . Or, to install in editable mode while hacking on the code:
pip install -e . Alternatively, install only the dependencies and run the script directly:
pip install -r requirements.txt python3 aic.py ... Note: The py-landlock package is only used on Linux and is optional — the agent runs unsandboxed if it is not installed.
AI-Commander is a single-file script. Run it from the repository root with python3 aic.py, or use the aic command installed by the aic-agent package:
# Installed via pip (package: aic-agent) aic \ --api-base https://api.example.com/v1 \ --model your-model-name \ --api-key YOUR_API_KEY \ "your task request" # From a repository checkout python3 aic.py \ --api-base https://api.example.com/v1 \ --model your-model-name \ --api-key YOUR_API_KEY \ "your task request" The TUI starts with no request and waits for you to type one in the prompt panel. In --nogui mode a request is required on the command line.
# Run in the TUI (default) python3 aic.py --api-base https://api.example.com/v1 \ --model gpt-4o --api-key sk-... "List all files in this directory" # Run in plain CLI mode python3 aic.py --nogui --api-base https://api.example.com/v1 \ --model gpt-4o --api-key sk-... "Summarize the contents of aic.py" # Auto-approve all commands (useful for testing / scripting) python3 aic.py --auto-approve --api-base https://api.example.com/v1 \ --model gpt-4o --api-key sk-... "Set up a new Python project" # Use the truncation algorithm and a smaller, faster prompt python3 aic.py --fast --api-base https://api.example.com/v1 \ --model gpt-4o --api-key sk-... "List all files" How it works The agent receives your task request. It plans the next step and may emit thinking tokens. It proposes shell commands, which you approve or auto-approve. Commands run (optionally sandboxed and time-limited), and their output is fed back to the model. The loop repeats until the task is considered complete. All I/O is routed through the EventSink abstraction, so the same agent logic runs unchanged in both the TUI and --nogui modes.
The agent exposes a single tool, execute_bash(command), which runs the command in a pseudo-terminal (PTY). The PTY gives the command a real terminal environment (so interactive tools and tput-style programs work) and lets the agent capture output, enforce a timeout, and kill the whole process group when needed. Output is streamed live to the console panel and the final result is truncated to --max-output-bytes with the literal sentinel output too long: truncated appended when the limit is exceeded.
The default Textual interface is a Norton-Commander-style split screen:
The agent runs in a background thread and communicates with the UI through a queue; the UI never blocks on the agent.
Type any of the following in the prompt panel (they are UI controls, so they are not added to prompt history):
Note: When a command is pending approval, you can also approve/reject it with the modal dialog (Y/N keys) or the on-screen buttons. The /approve, /reject, and /suggest commands are an alternative to the dialog.
Prompt history is persisted to ~/.aic_history (up to 1000 entries) and loaded at startup. Up/Down navigation preserves your in-progress line (readline semantics), and Ctrl+R opens a modal reverse search that filters history newest-first; Ctrl+R cycles to the next older match, Enter accepts a match back into the prompt, and Escape/Ctrl+G cancels.
By default every command the agent proposes must be approved before it runs. This keeps you in control of what executes on your system.
You can toggle auto-approve at any time with /autoapprove (or /aa), or start with --auto-approve. When auto-approve is ON, all commands run without asking — useful for testing and scripting, but be ready to /stop if a command misbehaves.
The prompt budget is --max-prompt-len (default 80,000 tokens). For models that accept a max_tokens request parameter (all non-gpt models), the budget is reduced by the output reservation (max_tokens, 8,000) so the prompt leaves room for the completion inside the context window:
prompt_budget = max_prompt_len - max_tokens (for non-gpt models) prompt_budget = max_prompt_len (for gpt models) Token counts are estimated locally with a conservative heuristic (~3 characters per token plus per-message overhead). Exact numbers come from the API's usage stats once each call finishes and are shown in the status bar.
--compress-alg truncate (or --fast) is the simpler, cheaper algorithm. It always preserves the system prompt (index 0) and the first user instruction (index 1) so the agent never forgets its primary objective. It works in two passes:
Both passes stop once the retained history fits within --compress-target (default 40%) of the budget, leaving headroom so several new turns fit before the next compression. Truncating down to the full budget would make every following prompt exceed the limit again and re-compress on each step, slowly losing more history than necessary.
--compress-alg context-compressor-llm (the default) uses the context-compressor-llm package, which implements a Factory.ai-style anchored-summary incremental compressor. When the non-system log exceeds the budget it:
The system prompt is kept byte-identical at the front of the retained context so unchanged prefixes are never re-prefilled (important on systems with slow prompt processing / prefix caching). The first user instruction is also preserved verbatim, and a fallback user message is appended if aggressive compression would leave no user query at all (some endpoints reject that).
If the context-compressor-llm package is not installed, this algorithm falls back to truncate.
Use --compress-target to control how much headroom is retained (default 0.4 = 40%). A higher value keeps more history but triggers compression sooner; a lower value compresses harder but loses more context.
AI-Commander mirrors the complete conversation history to a JSON file and reloads it on every startup, so nothing already produced is lost to a crash, a kill, a closed terminal, or Ctrl+C.
The status line at startup reports what happened, e.g. [SESSION] Persisting session to /project/.aicsession or [SESSION] Restored 14 message(s) from .aicsession (session 20260828_134851, step 4).
Plain, indented, UTF-8 JSON — deliberately simple enough to read and edit:
{ "aic_session_version": 1, "session_id": "20260828_134851", "updated_at": "2026-08-28T13:52:58", "reason": "tool_result", "model": "gpt-4o", "api_base": "https://api.example.com/v1", "cwd": "/home/me/project", "instance_number": 1, "step_count": 4, "usage": { "input_tokens": 5321, "output_tokens": 812, "cached_tokens": 4096, "billable_tokens": 2037 }, "messages": [ { "role": "system", "content": "You are an expert planning..." }, { "role": "user", "content": "summarize this repo" }, { "role": "assistant", "content": null, "tool_calls": [ { "id": "call_1", "type": "function", "function": { "name": "execute_bash", "arguments": "{\"command\": \"ls\"}" } } ] }, { "role": "tool", "tool_call_id": "call_1", "content": "aic.py\nREADME.md" } ] } messages is the raw OpenAI message list, saved exactly as the agent holds it (same roles, tool_calls, and tool_call_ids). reason records which checkpoint produced the file (user_request, before_llm_call, assistant_message, tool_result, loop_terminated, process_exit, signal_15, interrupted, cleared, …), which makes it easy to tell where a crashed run stopped.
Because the messages array is stored verbatim, a session can be reshaped with any text editor or JSON tool:
# drop the last 6 turns, then resume python3 - <<'EOF' import json d = json.load(open(".aicsession")) d["messages"] = d["messages"][:-6] json.dump(d, open(".aicsession", "w"), indent=2) EOF python3 aic.py --api-base ... "continue from here" Change, insert, or delete turns; edits are picked up on the next startup, or immediately inside the TUI with /reload. A bare JSON array of messages is accepted in place of the full object, so you can paste a transcript straight into the file. Invalid content is repaired rather than rejected: a missing system prompt is prepended, orphan tool messages are dropped, and a trailing assistant turn with no tool calls is removed (the API rejects prompts ending on an assistant turn). Each repair is reported so nothing changes silently. If the file ended on an assistant turn that did issue tool calls (the process died mid-command), the tool calls are answered with a [SESSION RECOVERED] notice stating the command was not re-executed — the agent is told not to assume it had any effect, rather than replaying it. Multiple instances in one directory Each process claims its session file with an flock on a sidecar .aicsession.lock (the data file itself is atomically replaced, so the lock lives beside it). Launching a second AI-Commander in the same directory warns and shifts to a numbered file instead of clobbering the first:
[SESSION] WARNING: another AI-Commander instance is already using .aicsession in this directory; this instance uses .aicsession2 instead (instance 2) Third instance → .aicsession3, and so on. The lock is released by the kernel when a process exits or dies, so a crashed run can never permanently squatter a name. --session participates in the same numbering ( 2, 3, …).
At most the single in-flight turn — never earlier history — can be missing, and the only thing never replayed automatically is a shell command that had not finished when the process died.
By default, on Linux, the agent attempts to enable an OS-level Landlock sandbox using py-landlock. When active, writes are restricted to the current working directory, /tmp, /dev, and /dev/pts. The status is shown in the agent output panel (green when active, red when unsandboxed).
The sandbox is applied before the agent thread starts, so the Landlock domain covers the whole process — including every command the agent runs.
The sandbox is only applied on Linux. On other platforms (macOS, Windows) the agent runs unsandboxed and reports that status at startup.
Tip: The status bar and the startup banner show whether the sandbox is active. If you see [red]Sandbox: off[/red], either you passed --disable-sandbox, py-landlock is missing, or you are not on Linux.
--fast is designed for local inference and low-latency setups. It:
This reduces prompt size and latency at the cost of less-detailed operating instructions for the model.
├── aic.py # The entire agent (single-file implementation) ├── requirements.txt # Python dependencies ├── README.md # This file ├── tests/ # Test suite (truncation + core helpers) └── LICENSE # Apache 2.0 License This project is licensed under the Apache License 2.0.
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.




