Building a coding agent in Swift from scratch
TL;DR Highlight
Rebuilding a Claude Code-style agent in Swift shows a few tools and a tight loop go far
Who Should Read
A developer using LLM APIs who wants to understand coding agent internals by rebuilding one stage by stage
Core Mechanics
- Hypothesis: few, good tools win
- The author credits Claude Code's quality to restraint.
- The bet is that a few excellent tools and a tight loop beat large orchestration layers.
- There is only one agent loop
- Send messages and return the text if stop_reason is not tool_use.
- Otherwise run the tools, append tool_result as a user message, and loop again.
- The loop never changes across stages
- Each stage only adds entries to the tool handler dictionary and injection points before the API call.
- The loop body is identical in every stage.
- Phase 1 is the loop plus basic tools
- Stage 01 is the loop with bash, and 02 adds read_file, write_file and edit_file with path safety.
- Stage 03 adds todo tracking with nag reminder injection.
- Phase 2 adds product features
- 04 is subagents (recursive loop with fresh context) and 05 is skill loading (.md injected as tool results).
- 06 is 3-layer context compaction (micro, auto, manual), 07 a file-based task system with dependency DAG, and 08 background tasks.
- It calls the Anthropic Messages API directly
- It sends raw HTTP to POST https://api.anthropic.com/v1/messages via AsyncHTTPClient.
- It uses Swift 6.2 with strict concurrency and runs on macOS and Linux.
Community Reactions
- Context management is the hard part, one commenter says
- Accumulated tool call history reportedly degrades quality well before the hard context limit.
- They saw "let me check that again" loops and hedged tool selection.
- Three fixes one commenter tried
- They replaced full tool history with a compact working-memory summary of finished sub-tasks.
- They dropped intermediate file reads once info was extracted and described what "done" looks like in the system prompt.
- Swift fits agents well, one opinion goes
- Structured concurrency reportedly maps well to the agent loop.
- The strong type system makes tool schemas less error-prone than JSON string wrangling.
- Naming and alternatives came up in comments
- One commenter warned that naming the CLI 'claude' could invite legal trouble.
- Others shared Operator, a Swift agent loop library, and a Python+rich agent whose UI broke on window resize.
How to Apply
- Audit your agent's tool list
- Today, list your tool definitions, merge overlapping ones and center on search and file-edit tools.
- Run the same task before and after to compare results.
- Separate the loop from extension points
- Keep the loop to a stop_reason check and appending tool_result, and move features into a tool handler dictionary.
- Attach new features at injection points before the API call.
- Add context compaction for long sessions
- Replace finished sub-task outputs with a summary block and drop file reads once used.
- Add a short "definition of done" paragraph to the system prompt.
- Follow the repo tag by tag
- Check out the 01-agent-loop tag and run the bash-tool loop.
- Move through later tags to see each stage's added feature.
Try It
# Run in Claude Code or a terminal (needs Swift 6.2 and an Anthropic API key)
git clone https://github.com/ivan-magda/swift-coding-agent.git
cd swift-coding-agent
cp .env.example .env
# Put ANTHROPIC_API_KEY and MODEL_ID in .env
git checkout 01-agent-loop
swift build
swift run agentTerminology
Related Papers
Frostbite: A new CPU architecture discovered, designed and verified by AI
AI agents designed a CPU from scratch, claiming 4.59x perf/W over a traditional core
Restricting AI agents in mobile operating systems platforms from capturing data
Signal now blocks OS-level AI agents from reading its screen on iOS 27 and Android
Actions Speak Louder than Words: Stateful Privacy Sniffing of LLM Agents via Behavioral Covert Channels in Web Interactions
Agents' link choices alone let attackers rebuild user queries at F1 0.89; SOR cuts it to 0.39.
Tracking and Limiting Unsupported Claim Propagation in Multi-Agent LLM Systems: A Provenance-Aware Framework
Passing claims with source, evidence and history kept equivalence at 5 agent stages at 96% versus 31%.
LLM assisted reverse engineering
Pointing an agent at binaries with skills and MD notes made years-long hacks feasible
Metrics-in-the-Loop: Complexity Feedback Reshapes Agent-Written Code
Injecting per-turn complexity reports into coding agents cut code erosion without changing correctness
Why is Gemini 4 Carbon taking so long?
Carbon took nearly an hour on bug fixes that Astra and Opus finished in minutes, mostly from extra unrequested work.
Weight-Space Programming builds neural networks like a 1990s researcher
LLM hand-wrote MNIST weights to 91.3%, using 1990s-style techniques