A Rust-based autonomous coding agent that runs an interactive loop with an LLM, using tools for file I/O, planning, tasks, background execution, team messaging, subagent delegation, and git worktree isolation.
Code index: DeepWiki — swiftugandan/nano-agent
Original inspiration: shareAI-lab/learn-claude-code — Bash is all you need: a nano Claude Code–like agent, built from 0 to 1
- Rust (2021 edition)
- LLM API keys (one of):
- Anthropic:
ANTHROPIC_API_KEY(default backend) - OpenRouter:
OPENROUTER_API_KEY— setLLM_BACKEND=openaito use
- Anthropic:
# 1. Build
cargo build --release
# 2. Set an API key (pick one)
export ANTHROPIC_API_KEY="sk-ant-..." # default backend
# — or —
export LLM_BACKEND=openai
export OPENROUTER_API_KEY="sk-or-..."
export OPENROUTER_MODEL="openai/gpt-4o-mini" # any OpenRouter model
# 3. Run
./target/release/agentInteractive (REPL) — launch with no arguments:
agentThe REPL provides readline-style editing (arrow keys, Ctrl-A/E/K), persistent history (up arrow recalls previous inputs across sessions), and tab-completion for slash commands (type / then Tab).
- Ctrl-C — cancel current input, show new prompt
- Ctrl-D — clean exit (same as
/quit)
One-shot — pass a prompt as an argument:
agent "explain src/main.rs in one paragraph"Prints the response to stdout and exits. Useful for scripting and pipes:
cat error.log | agent "what went wrong?"
agent "list TODOs in src/" > todos.txtResume a session:
agent --resume <session-id>Transcripts and history are stored under .nano-agent/ in the current working directory.
| Variable | Default | Description |
|---|---|---|
LLM_BACKEND |
anthropic |
anthropic or openai |
AGENT_NAME |
lead |
Agent identity name |
AGENT_ROLE |
developer |
Agent role in system prompt |
SKILLS_DIR |
.nano-agent/skills |
Directory for skill files (see read_skill) |
OPENROUTER_API_KEY |
— | API key for OpenRouter backend |
OPENROUTER_BASE_URL |
https://openrouter.ai/api/v1 |
OpenRouter API base URL |
OPENROUTER_MODEL |
openai/gpt-4o-mini |
Model to use via OpenRouter |
ANTHROPIC_API_KEY_2..10 |
— | Additional API keys for rotation on auth errors |
RETRY_MAX_ATTEMPTS |
3 |
Max retry attempts for transient LLM errors |
RETRY_BASE_DELAY_MS |
1000 |
Base delay (ms) for exponential backoff |
RESUME_SESSION |
— | Session ID to resume on startup |
Data is stored under .nano-agent/ in the current working directory: tasks, transcripts, inbox, team, events.jsonl, and skills.
In the REPL you can use:
/quit— Exit and save transcript/clear— Save transcript, clear conversation, start fresh/status— Show token estimate, turn count, todo list, background completions/tasks— List all tasks (task manager)/team— List teammates/events— Show recent event-bus entries/resume— List available sessions for resumption
The agent has many tools, including:
- File I/O — read, write, search, list files
- Planning —
todo_update,todo_read(single in-progress item) - Tasks —
task_create,task_get,task_update,task_list(with dependencies) - Background —
background_run,background_check - Team —
send_message,broadcast_message,scan_tasks,claim_task - Delegation —
subagent(isolated subtask with child LLM call) - Isolation —
worktree_create(git worktree per task) - Knowledge —
read_skill(load skills fromSKILLS_DIR) - Events —
emit_event,list_events - Memory —
save_memory(persist context for TF-IDF recall) - Lifecycle —
idle(transition to idle),compact(manual compaction) - Cron —
cron_add,cron_list,cron_remove - Team —
spawn_teammate,shutdown_teammate - Delivery —
enqueue_delivery(reliable message delivery with retry)
The system prompt instructs the agent to keep one todo in progress, use tasks for multi-step work, use background runs for long commands, and coordinate via the message bus and task claiming.
cargo build # debug build
cargo build --release # optimized buildcargo test # run all tests
cargo test test_l1 # run a specific test suite (e.g. core loop)
cargo test scenario_19 # run a single scenarioDelegation progress is only shown in interactive REPL mode (not in one-shot).
-
Automated: Progress phases are covered by:
cargo test test_l3_06 -
Subagent in REPL: Start the agent, then ask it to delegate a small task so it uses the
subagenttool, e.g.:- "Use the subagent to read the first 5 lines of README.md and summarize."
You should see:
tool: subagent, then→ delegating to subagent: ..., then lines like└ subagent: picked up — started,└ subagent: working — step N,└ subagent: running tool: read_file,└ subagent: complete (...).
- "Use the subagent to read the first 5 lines of README.md and summarize."
You should see:
-
Teammate delegation: If the agent sends a message to a teammate (e.g. "Send a message to specialist: analyze data in data.csv"), you should see after
tool: send_messagea line:→ delegated to specialist: .... -
Teammate status: Run
/statusto see a Teammates: section (working/idle). Run/teamfor the full teammate list.
- Define the tool schema in
src/tools.rs(tool_definitions()) - Add a handler closure in
src/main.rs(build_dispatch()) - Add tests in the appropriate
tests/file
Slash commands are handled in the REPL loop in src/main.rs. To add tab-completion for a new command, add it to the SlashCompleter command list in src/channels.rs.
See ARCHITECTURE.md for the full 11-layer design. The core invariant is a simple loop: call the LLM, append its response, execute any tool calls, append results, repeat until the LLM stops.
src/main.rs— Binary: REPL, tool wiring, system prompt, pre-turn pipeline (memory, nag, background, inbox).src/lib.rs— Library root; modules:- LLM —
anthropic,openai,types(e.g.Llmtrait) - Loop —
core_loop(run agent turn with tools) - Tools —
tools(definitions + dispatch),delegation(subagent) - Planning —
planning(todo, nag policy) - Tasks —
tasks(TaskManager, dependency graph) - Memory —
memory(compaction, transcript store, session replay),memory_store(TF-IDF semantic recall) - Concurrency —
concurrency(background runner) - Teams —
teams(message bus, teammates) - Protocols —
protocols(e.g. request tracking) - Autonomy —
autonomy(identity, task scan/claim) - Isolation —
isolation(event bus, worktree manager) - Knowledge —
knowledge(skill loader) - Channels —
channels(Channel trait, CliChannel, ChannelManager),gateway(WebSocket server, routing) - Delivery —
delivery(write-ahead queue, background retry runner) - Resilience —
resilience(ResilientLlm, ContextGuard, retry policy) - Prompt —
prompt(PromptAssembler, layered composition) - Util —
util(shared helpers) - Mock —
mock(test doubles for LLM)
- LLM —
See repository license (if any).