The smallest practical coding agent in Rust
Single binary • ~185 LOC • blocking HTTP • no framework ceremony
- Tiny by design: nearly everything lives in
src/main.rs - Action-first agent behavior: built to execute, not narrate
- OpenAI-compatible API: works with OpenRouter or any compatible
/chat/completionsendpoint - Easy to extend: add a tool in two spots (
call_api()+dispatch())
flowchart TD
A[you] --> B[agent loop]
B --> C[LLM API]
C --> D{tool_calls?}
D -->|yes| E[shell / read_file / write_file]
E --> F[tool results]
F --> C
D -->|no - end_turn| G[print response]
-
load_env()
Reads.envon startup and sets environment variables (manual parser, no dotenv crate). -
call_api()
Sends full conversation history to an OpenAI-compatible/chat/completionsendpoint and registers 3 tools:shell,read_file,write_file. -
main()agent loop- Outer loop: reads your prompt and appends a
usermessage. - Inner loop: calls model → executes tool calls → appends
toolmessages → repeats until end turn.
- Outer loop: reads your prompt and appends a
The system prompt explicitly pushes the model to execute work:
"Never describe what you would do. Do it."
"When asked to build something: create the files, run them, fix errors, confirm success."
user: { role: "user", content: "your prompt" }
assistant: { role: "assistant", tool_calls: [{id, function: {name, arguments}}] }
tool: { role: "tool", tool_call_id: id, content: "cmd output" }
assistant: { role: "assistant", content: "final answer" }
The model decides when to call tools and when to stop.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env
git clone https://github.com/Engineering4AI/nano-code
cd nano-codecp .env.example .env
# edit .env with your API keycargo runcargo build --release
./target/release/nano-code| Variable | Default | Description |
|---|---|---|
OPENROUTER_API_KEY |
required | API key |
INFERENCE_BASE_URL |
https://openrouter.ai/api/v1 |
Any OpenAI-compatible base URL |
MODEL_NAME |
anthropic/claude-sonnet-4-6 |
Model identifier |
| Crate | Purpose |
|---|---|
reqwest (blocking) |
HTTP client |
serde + serde_json |
JSON serialization |
No additional framework layers.
- mini-swe-agent — Python alternative
If you use nano-code in research, please cite:
@software{nano_code2026,
title = {nano-code: The Smallest Possible Coding Agent in Rust},
author = {{Engineering4AI}},
year = {2026},
url = {https://github.com/Engineering4AI/nano-code}
}