Swift Claude Code

A Swift reimplementation of a Claude Code-style coding agent, built stage by stage to explore what makes coding agents work

Last verified:

Visit Swift Claude Code

What is Swift Claude Code?

Swift Claude Code is a open-source coding agent built from scratch in Swift that replicates the core mechanics of Anthropic's Claude Code. It is a terminal-based AI coding assistant that enables autonomous code editing, file operations, and command execution through a small set of tools and a simple agent loop. The project is built stage-by-stage in a 9-part series, with each stage adding one capability to a core that never changes.

Key features include: an agent loop (the core invariant kernel) that drives autonomous execution, tool dispatch system supporting 14+ tools (read_file, write_file, edit_file, bash, grep, todo_write, todo_read, subagent, skill loading, and more), todo persistence with file-based state, subagents for spawning child agents on scoped tasks, dynamic skill loading for capability injection, context compaction to summarize conversation history, task DAG for dependency-ordered execution, and background tasks via Swift actors. The codebase uses Swift 6.2 strict concurrency for compile-time data race detection and AsyncHTTPClient for cross-platform HTTP requests.

This tool is for Swift developers who want to understand how coding agents work, educators teaching AI agent architecture, developers building custom AI coding assistants in Swift, and anyone interested in the design trade-offs behind Claude Code's effectiveness. It serves as both a functional CLI tool and an educational series demonstrating architectural restraint—a small set of excellent tools, thin orchestration, and heavy reliance on the model itself.

Swift Claude Code pricing

Pricing model: Freemium

Free and open-source under no licensing restrictions. The only cost is the Anthropic API key required to use Claude's models (claude-sonnet-4-6 or similar). Users pay Anthropic directly for API usage based on their token consumption. No paid plans, subscriptions, or Premium tiers for the Swift Claude Code tool itself.

Swift Claude Code pros

  • Built entirely in Swift 6.2 with strict concurrency checking catches data races at compile time
  • Open-source with complete 9-part tutorial series explaining every design decision
  • Minimal architecture—only 20 lines of Swift for the core agent loop
  • Two-target Swift Package Manager layout (Core library + cli executable) enables unit testing
  • Uses AsyncHTTPClient for reliable cross-platform HTTP on macOS and Linux
  • Bash tool serves as universal interface to OS—read/write files, run tests, git, search codebase
  • Dictionary-based tool dispatch scales to 14+ tools without switch statements
  • Subagents enable context isolation through recursive agent loops for scoped tasks
  • Todo persistence tool prevents agents from losing their plan during long tasks
  • Context compaction summarizes conversation history to stay within token limits
  • Task DAG enables dependency-ordered execution for complex multi-step workflows
  • Background tasks via Swift actors enable parallel execution without blocking
  • No external AI SDK dependency—builds raw HTTP client with AsyncHTTPClient
  • Swift Testing framework supports async test functions on macOS and Linux
  • ENV-based API key configuration with .env.example documentation

Swift Claude Code cons

  • Requires Anthropic API key and paid Claude model access to function
  • Only 14 tools currently implemented vs. Claude Code's full toolset
  • No GUI—terminal-only CLI interface may not suit all developers
  • Still experimental educational project, not production-ready for critical work
  • Context window limit still applies despite compaction feature
  • Raw HTTP implementation means no built-in streaming or retry logic yet
  • Linux support depends on Swift 6.2 which may not be stable on all distros
  • No built-in MCP (Model Context Protocol) integration documented

Frequently asked questions about Swift Claude Code

What is Swift Claude Code?

Swift Claude Code is a custom implementation of a Claude Code-style coding agent built entirely in Swift from scratch. It replicates the core mechanics of Anthropic's Claude Code—a terminal-based AI assistant that autonomously reads files, writes code, runs commands, and iterates until tasks are complete—using Swift 6.2 and Swift Package Manager instead of JavaScript/Node.js.

How does the agent loop work?

The agent loop sends the user's query plus conversation history to the Claude API, checks if the model requested a tool use (stopReason == .toolUse), executes the bash command if requested, appends the result to messages, and loops back. This continues until the model returns end_turn instead of tool_use. The entire mechanism fits in one method with a single guard statement as the branching point.

What tools does Swift Claude Code support?

The toolset includes: bash (universal shell interface), read_file, write_file, edit_file, grep, todo_write, todo_read, subagent for spawning child agents, skill loading for dynamic capabilities, and more—totaling 14 tools in Stage 2. Tools are dispatched via a dictionary rather than a switch statement for scalability.

Why use bash as the only tool initially?

Bash serves as a universal interface to the operating system—it can read files, write files, search codebases, run compilers, execute tests, install packages, and manage git. Giving the model just bash with the freedom to construct any command is more powerful than hard-coding specific file operations, and the model decides what commands to run.

How do I install and run Swift Claude Code?

Clone the repo at github.com/ivan-magda/swift-claude-code, create a .env file with ANTHROPIC_API_KEY and MODEL_ID=claude-sonnet-4-6, run swift build to compile (first build takes 1-2 minutes for dependencies), then run swift run agent. The CLI presents a REPL prompt where you type queries and the agent executes them autonomously.

What Swift version is required?

Swift 6.2 is required to enable strict concurrency checking by default, which catches data races at compile time. This is critical for the later stages that add background tasks via Swift actors and parallel execution. The Package.swift explicitly declares swift-tools-version: 6.2.

How does context compaction work?

Context compaction (Stage 6) summarizes conversation history to prevent hitting the token limit. As the messages array grows without bound during long sessions, the agent compresses older exchanges into summaries while preserving important details, allowing the agent to continue working without losing memory of earlier decisions.

What are subagents used for?

Subagents (Stage 4) spawn child agents for scoped tasks, enabling context isolation. When a task requires focused work in a specific area (like refactoring a single module), a subagent handles that task without polluting the parent agent's context with irrelevant details, improving efficiency and preventing context bloat.

Does it work on Linux?

Yes, Swift Claude Code works on both macOS and Linux thanks to AsyncHTTPClient built on SwiftNIO, which handles async responses identically across platforms. URLSession's async APIs were inconsistent on Linux until recently, which is why AsyncHTTPClient was chosen instead. Swift 6.2 must be available on the Linux distribution.

How is this different from Claude Code by Anthropic?

Swift Claude Code is an educational reimplementation built from scratch in Swift to understand what makes coding agents work, while Claude Code is Anthropic's production CLI tool. Swift Claude Code has fewer tools (14 vs. many more), no GUI, relies on raw HTTP instead of the official SDK, and prioritizes teaching architecture over feature completeness. The thesis is that architectural restraint—not feature bloat—is what makes agents effective.

Categories

Use cases

Browse all AI tools on NeedAnAI