A coding agent that runs in your terminal, written from scratch in Python to learn how harnesses like Claude Code and Codex work underneath.
You give it a task. It streams the model's reply, calls tools (read, edit, grep, shell, web), feeds the results back, and loops until the task is done or it hits the turn limit.
$ python main.py "find where we parse the config and add a debug flag"
What's inside
- Agent loop (
agent/): async, streaming, multi-turn tool calling over any OpenAI-compatible API. Events (text deltas, tool calls, tool results, errors) are emitted to the UI as they happen. - 12 built-in tools (
tools/builtin/):read_file,write_file,edit,list_dir,glob,grep,shell,web_search,web_fetch,todo,memory, plus subagents (a built-incode_reviewerand any you define in config). Every tool has a Pydantic schema, so the model gets a JSON schema and bad arguments get rejected before anything runs. - MCP support (
tools/mcp/): connect stdio or HTTP MCP servers from config. Their tools show up next to the built-in ones. - Subagents: define a specialist in TOML with its own goal prompt and an allowlist of tools. The main agent hands it a goal and gets back the result, without the subagent's tool calls filling up the main context.
- Context management (
context/):- Token counting with
tiktoken. - Compaction: past 80% of the model's context window, the conversation is summarized by the model and replaced with that summary.
- Pruning: old tool outputs are cleared once they add up past a threshold, keeping the most recent ~40K tokens of tool output intact.
- Token counting with
- Shell environment policy: env vars matching
*KEY*,*TOKEN*,*SECRET*are stripped before a shell command runs. - Project context: an
AGENT.MDin the working directory is loaded into the system prompt. - Persistent memory: the
memorytool stores key/value notes in your user data dir, so they survive across sessions. - Terminal UI (
ui/): Rich-based streaming output with tool calls rendered as they run.
In progress on a branch: a policy-based approval layer (on-request, auto-edit, never, yolo, ...) that asks before running risky shell commands or edits.
Run it
Requires Python 3.11+.
pip install openai click rich pydantic platformdirs python-dotenv tiktoken httpx ddgs fastmcp
Point it at any OpenAI-compatible endpoint (OpenRouter, a local server, etc.) with a .env:
API_KEY=...
BASE_URL=https://openrouter.ai/api/v1
python main.py # interactive session
python main.py "explain agent/agent.py" # one-shot
python main.py -c ../other-repo # run against another directory
Configure
Config is TOML, merged in this order: user config (~/Library/Application Support/krypto/config.toml on macOS) then project config (.krypto/config.toml).
[model]
name = "z-ai/glm-4.5-air:free"
temperature = 0
context_window = 256000
[subagents.codebase_investigator]
description = "Investigates the codebase to answer questions about code structure"
goal_prompt = """You are a codebase investigation specialist.
Use read_file, grep, glob, and list_dir to investigate. Do NOT modify any files."""
allowed_tools = ["read_file", "grep", "glob", "list_dir"]
[mcp_servers.filesystem]
command = "npx"
args = ["-y", "@modelcontextprotocol/server-filesystem", "."]
Other options: max_turns (default 100), allowed_tools to restrict the agent, shell_environment.exclude_patterns / set_vars.
Layout
agent/ loop, session, event types
client/ streaming OpenAI-compatible client with retries
context/ message history, token tracking, compaction, pruning
tools/ tool base class, registry, built-ins, MCP bridge
prompts/ system and compaction prompts
config/ Pydantic config + TOML loader
ui/ Rich terminal UI