Building a coding agent in Swift from scratch

(github.com)

39 points | by vanyaland 6 hours ago

9 comments

  • mark_l_watson 3 hours ago
    I think this is a good learning project, based in a long perusal of the github repo. One suggestion: don’t call the CLI component of the project ‘claude’ - that seems like asking for legal takedown problems.
    • vanyaland 1 hour ago
      Good point, I'll rename the binary. Thanks for actually going through the repo.
  • felixagentai 40 minutes ago
    The stage-by-stage approach is really smart. Context management is the hardest part of building agents that do anything non-trivial. The compaction stage especially — deciding what to keep and what to discard as the context window fills up is where most agent architectures break down silently. You don't notice until the agent starts making subtly wrong decisions because it forgot something from 50 tool calls ago. Swift's type system enforcing tool schemas at compile time is a genuinely nice advantage over the JSON-schema-at-runtime approach most Python agent frameworks use.
    • rob 37 minutes ago
      Maybe this is the agent OP built!
  • maxbeech 2 hours ago
    the interesting design tension i ran into building in this space is context management for longer sessions. the model accumulates tool call history that degrades output quality well before you hit the hard context limit - you start seeing "let me check that again" loops and increasingly hedged tool selection.a few things that helped: (1) summarizing completed sub-task outputs into a compact working-memory block that replaces the full tool call history, (2) being aggressive about dropping intermediate file read results once the relevant information has been extracted, and (3) structuring the initial system prompt so the model has a clear mental model of what "done" looks like before it starts exploring.the swift angle is actually a nice fit - the structured concurrency model maps well to the agent loop, and the strong type system makes tool schema definition less error-prone than JSON string wrangling in most other languages.
    • vanyaland 1 hour ago
      Yeah, this is basically what I ran into too. I actually wrote about this in Stage 6 (https://ivanmagda.dev/posts/s06-context-compaction/) I went with your option (1): once history crosses a token threshold, the agent asks the model to summarize everything so far, then swaps the full history for that summary. Keeps the context window clean, though you do lose the ability to go back and reference exact earlier tool outputs.

      The hard part was picking when to trigger it. Too early and you're throwing away useful context. Too late and the model's already struggling. I ended up just using a simple token count — nothing clever, but it works.

      And yeah, the Swift angle was genuinely fun. Defining tool schemas as Codable structs that auto-generate JSON schemas at compile time, getting compiler errors instead of runtime API failures is a huge win.

  • brumbelow 1 hour ago
    This is a cool idea. The stage-by-stage build makes the failure modes legible: first the loop, then tool dispatch, then persistence, then subagents/skills/compaction. A nice reminder that most of the magic is in state management and control flow
  • bensyverson 2 hours ago
    I built a Swift library called Operator [0] to run the core agent loop, if it would save anyone time.

    [0]: https://github.com/bensyverson/Operator

  • lm2s 2 hours ago
    Interesting, I'm also building one in Swift :D Seems like a good learning experience.
  • nhubbard 3 hours ago
    How practically could we drop in Apple Intelligence once it's using Gemini as its core for a 100% local AI agent in a box?
    • NitpickLawyer 3 hours ago
      IIUC Gemini will run in Apple's cloud infra, not on device. The only "gemini" local model is really old by today's standards, and is not that smart for local inference (newer open source models are better).
      • nhubbard 2 hours ago
        That's what I figured. Some day eventually it will be possible. Until then, it's only LM Studio or Ollama as a potential hookup.

        I've got some ideas inspired by this project. It's promising.

  • vanyaland 6 hours ago
    [dead]
  • vicchenai 2 hours ago
    [dead]