How sentdex/minion works
Purpose and Entry Points
Minion is a single-process Python CLI coding agent optimized for minimal LLM context overhead. Its implementation is intentionally centralized in minion.py; it starts an interactive terminal REPL, or supports session-listing/resume CLI modes. Packaging exposes the minion command through setup.py and project metadata in pyproject.toml.
Paths: minion.py, setup.py, pyproject.toml
Runtime and Backend Boundary
The process loads environment-based source configuration, including ~/.env, then creates an OpenAI SDK client for an OpenAI-compatible endpoint. Sources may be local llama.cpp, vLLM, SGLang, remote OpenAI-compatible APIs, Together, OpenRouter, or LiteLLM proxies in front of Bedrock. Source switching preserves conversation history while changing endpoint/model behavior.
Paths: minion.py, sources.example.env, requirements.txt
REPL and Conversation Orchestration
The main control loop combines a raw-terminal multiline editor (with non-TTY fallback), slash-command dispatch, status output, prompt history, and streamed model turns. It maintains the model-visible message array and drives iterative tool-call turns until the model produces a user-visible final response or recovery returns control to the user.
Paths: minion.py
Model Streaming, Tool Protocol, and Recovery
Streaming responses are consumed directly from the OpenAI API, including optional reasoning_content. Native function calls are preferred; unsupported servers use parsed standalone [minion_tool_call] text blocks. Tool-result delimiters are escaped before re-entering context. Guards recover from malformed SSE/tool arguments, empty turns, repetition failures, and reasoning-only stalls through bounded retries and forced-final-answer requests.
Paths: minion.py, tests/test_text_tool_protocol.py, tests/test_empty_turn_recovery.py
Workspace Tool Execution and Approval Boundary
The agent exposes a deliberately small local capability surface: file reads, writes, exact-match edits, directory listings, and shell commands. State-changing operations and shell execution pass through a separate cheap-model risk classification call, then an interactive approval gate unless the active approval threshold or YOLO mode permits execution. Execution results are capped/deduplicated before being appended to LLM history, limiting context growth.
Paths: minion.py, tests/test_approval_env.py, tests/test_risk_context.py
Context Management
Minion tracks estimated/current context usage against a per-source/model maximum. It probes and caches context limits using local-server metadata where available or an over-limit chat request for remote endpoints. Manual compression and automatic threshold-based compression summarize older conversation history while retaining recent turns verbatim, preserving continued task context within constrained local-model windows.
Paths: minion.py, tests/test_context_window.py, tests/test_autocompress.py
Persistence, Observability, and External Reporting
Each conversation is auto-saved as human-readable session JSON, including messages, source/cwd metadata, descriptions, and cumulative normalized token usage. Resume restores both transcript and originating source. Requests/SSE chunks are logged locally to llamacpp.log; an optional metrics URL receives fire-and-forget cumulative usage posts and disables after failure.
Paths: minion.py, tests/test_sessions.py
Main entry points: minion.py Turn orchestrator · setup.py Package metadata · pyproject.toml Project metadata · sources.example.env Source env template
Components 20
CLI Entry
- Package metadata · Installs the minion console command.
setup.py - Project metadata
pyproject.toml - CLI modes · Starts REPL or lists and resumes sessions.
minion.py - Source env template
sources.example.env - Runtime dependencies
requirements.txt
Agent Runtime
- Source configuration · Loads endpoint and model settings, including ~/.env.
minion.py - Interactive REPL · Handles multiline input, slash commands, history, and status.
minion.py - Turn orchestrator · Maintains messages and iterates model and tool turns.
minion.py
Model and Context
- OpenAI-compatible client · Switches sources while preserving conversation history.
minion.py - Streaming and recovery · Consumes SSE, parses reasoning/tool calls, and applies bounded recovery.
minion.py - Tool protocol · Uses native calls or parsed [minion_tool_call] blocks.
minion.py - Context manager · Tracks limits and compresses older history when needed.
minion.py - Context-limit probe · Caches local metadata or remote over-limit probe results.
minion.py
Workspace Safety
- Workspace tools · Reads, writes, edits, lists directories, and runs shell commands.
minion.py - Risk classifier · Classifies state-changing and shell operations.
minion.py - Approval gate · Applies threshold or YOLO policy before execution.
minion.py - Tool result control · Caps and deduplicates execution output before history insertion.
minion.py
Persistence and Reporting
- Session store · Autosaves transcripts, metadata, and normalized usage.
minion.py - Local request log · Records requests and stream chunks in llamacpp.log.
minion.py - Usage reporter · Posts cumulative usage and disables after failure.
minion.py
Connections 23
- Package metadata → CLI modes: exposes command
- Project metadata → Package metadata: defines project
- Source env template → Source configuration: documents settings
- Runtime dependencies → OpenAI-compatible client: provides SDK
- CLI modes → Interactive REPL: starts
- CLI modes → Session store: lists or resumes
- Source configuration → OpenAI-compatible client: configures endpoint
- Interactive REPL → Turn orchestrator: submits turns
- Turn orchestrator → Streaming and recovery: requests model turn
- Streaming and recovery → OpenAI-compatible client: streams API calls
- Streaming and recovery → Tool protocol: interprets calls
- Tool protocol → Workspace tools: invokes
- Workspace tools → Risk classifier: checks risky actions
- Risk classifier → Approval gate: requires decision
- Approval gate → Workspace tools: permits execution
- Workspace tools → Tool result control: returns output
- Tool result control → Turn orchestrator: adds tool result
- Turn orchestrator → Context manager: monitors history
- Context manager → Context-limit probe: uses limit
- Context-limit probe → OpenAI-compatible client: probes backend
- Turn orchestrator → Session store: autosaves
- Streaming and recovery → Local request log: logs chunks
- Session store → Usage reporter: reports usage