Building a coding agent in Swift from scratch
TL;DR Highlight
A learning project that reimplements the core architecture of Claude Code in Swift across 9 stages to understand why it works so well, directly validating the design philosophy of 'fewer tools, trust the model more.'
Who Should Read
Backend or iOS developers who want to build their own LLM-based coding agents, or developers who want to understand at a principled level how AI agents like Claude Code work internally.
Core Mechanics
- The core hypothesis of this project challenges the conventional wisdom that 'more tools make a better agent.' The author argues that Claude Code is effective not because of complex orchestration, but because of a small number of simple yet high-quality tools—like a search tool and a file editing tool—combined with a high degree of trust in the model.
- Five design principles for agent loop architecture are presented: ① A few high-quality tools > a vast tool catalog, ② thin orchestration (let the model decide directly), ③ explicit task state management improves reliability, ④ controlled context injection matters more than persistent memory, ⑤ Context Compaction is not just token savings—it's an actual product feature.
- The implementation is structured as a 9-stage learning series, with each stage designed to isolate and experiment with one mechanism—loop, tool dispatch, state management, sub-agents/skills/context compaction, etc. This allows you to see firsthand which architectural decisions actually make a difference.
- The choice of Swift as the implementation language goes beyond personal preference. Swift's Structured Concurrency (a structured concurrency model based on async/await that clearly manages task lifecycles) naturally aligns with agent loop architecture, and the strong type system reduces the JSON string parsing errors common in other languages when defining tool schemas.
- The project structure consists of Sources, Tests, docs, and skills/example directories, and includes an .env.example file and Package.swift for building directly with Swift Package Manager. A GitHub Actions workflow is also included.
- The author concludes that 'most of the magic is in state management and control flow'—meaning that the design of clearly controlling when and what an agent does matters more than the raw capability of the LLM itself.
- The full 9-part learning series is publicly available at ivanmagda.dev, so you can go beyond reading the code and also review the step-by-step design intentions and experimental results in written form.
Evidence
- "Real-world experience was shared that context management in long sessions is the hardest part. One commenter noted that well before hitting hard context limits, accumulating tool call history causes quality degradation—such as 'let me double-check' loops or ambiguous tool selection. Helpful mitigations included: ① summarizing completed subtask outputs and replacing them with compact working memory blocks, ② aggressively dropping intermediate file-read results after extracting the needed information, and ③ including a clear definition of 'done' state in the initial system prompt. There was also a comment raising legal concerns about naming the CLI component 'claude,' warning it could conflict with Anthropic's trademark and result in a takedown request. A developer shared a link to 'Operator' (github.com/bensyverson/Operator), a Swift library they built to handle the core parts of the agent loop, noting it could save time for others doing similar work. Comments praised the step-by-step build approach for making failure modes explicit—building up through loop → tool dispatch → state management → sub-agents/skills/compaction served as a reminder that 'most of the magic is in state management and control flow.' There was also a question about whether, once Apple Intelligence uses Gemini as its core, you could drop this project in to create a fully local AI agent—no direct answer was given, but it reflects interest in local model integration within the Swift ecosystem."
How to Apply
- "If you want to build your own coding agent like Claude Code, follow this repo's 9-stage series from the beginning and focus on implementing just one mechanism per stage (loop → tool dispatch → context management). This lets you isolate the cause of failures much faster than trying to implement everything at once. If you're experiencing quality degradation in long sessions, instead of keeping the entire tool call history in context, try replacing completed subtasks with summary blocks and immediately dropping file-read results once the information has been extracted. According to commenter experience, this significantly reduces quality issues that arise before hitting hard context limits. When designing a new coding agent, consider improving the quality of existing tools before adding more. As this project's hypothesis suggests, a single accurate search tool and a single accurate file editing tool can outperform 20 sloppy tools—something you can verify directly through experimentation. If you're implementing an agent in Swift, check out the 'Operator' library (github.com/bensyverson/Operator) mentioned in the comments first—it can reduce agent loop boilerplate code."
Terminology
Related Papers
Migrating a production AI agent to GPT-5.6: 2.2x faster, 27% cheaper
마케팅 웹사이트를 자동 생성하는 프로덕션 AI 에이전트를 Claude Opus 4.8에서 GPT-5.6 Sol로 전환한 실전 경험담으로, 단순 모델 교체가 아니라 eval 하네스, 툴 스키마, 캐싱, 추론 리플레이까지 손봐야 했던 과정을 구체적인 수치와 함께 정리했다.
What xAI's Grok build CLI sends to xAI: A wire-level analysis
xAI의 공식 코딩 CLI 도구 Grok Build가 사용자 동의 없이 전체 Git 저장소와 .env 시크릿 파일을 xAI 서버로 업로드한다는 사실이 네트워크 트래픽 분석으로 밝혀졌다.
Remember When It Matters: Proactive Memory Agent for Long-Horizon Agents
LLM 에이전트가 긴 작업 중 중요한 정보를 잊어버리는 문제를 별도의 메모리 에이전트가 '적절한 타이밍에' 끼어들어 해결하는 방법
WebSwarm: Recursive Multi-Agent Orchestration for Deep-and-Wide Web Search
복잡한 웹 검색을 재귀적으로 분해하고 각 노드에 적합한 검색 모드를 동적으로 할당하는 멀티에이전트 프레임워크
Show HN: Reverse-engineering web apps into agent tools
로그인된 웹 앱의 API 호출을 브라우저에서 감시해 자동으로 MCP 도구로 변환하는 에이전트를 만들었다. 소스 코드나 공식 API 문서 없이도 Jira, Spotify 같은 서비스에 AI 어시스턴트를 붙일 수 있다.
Show HN: FableCut – A browser video editor AI agents can drive (zero deps)
타임라인 전체를 JSON 파일 하나로 표현하고 MCP/REST로 AI 에이전트가 직접 편집할 수 있는 브라우저 비디오 에디터로, Claude 같은 AI가 프롬프트 하나로 영상을 자동 컷편집하고 결과를 실시간으로 UI에 반영해준다.