AI Engineering

From a Go agent loop to a self-hosted agent team: what you still have to build

You wrote the Go tool loop. Routing, @mention handoffs, approvals, persistence and schedules are next. When to keep writing Go and when to run a packaged team.

12 min read 2555 words
A single cyan loop on the left feeding into a network of linked bot nodes on the right, with a gate, a clock and a stacked data store along the path.

The agent loop is twenty lines of Go. I said so in Tool Use in Go and I still believe it. What I didn’t say is what happens the week after: you want a second bot, someone asks why it ran git push without asking, the process restarts and the conversation is gone, and now you’re writing a cron parser. The loop was the easy part. Everything around it is the actual project.

This post is the bridge between the Go AI posts on this blog and the kwant posts. First a quick recap of the loop, then an honest list of what you still have to build to turn one loop into a team of agents, then what you get if you run a packaged team instead, and finally when I’d keep writing Go anyway.

The kwant details below are checked against the kwant README, the kwant.si docs and the source as of November 19, 2026 (latest release 0.9.27).

Recap: the loop you already have

If you followed Calling an LLM from Go and the tool-use post, you have something like this at the core of your service:

for {
    resp, err := client.Messages.New(ctx, anthropic.MessageNewParams{
        Model:     "claude-opus-5",
        MaxTokens: 16000,
        Messages:  messages,
        Tools:     tools,
    })
    if err != nil {
        return err
    }

    // The assistant turn goes into history before the results.
    messages = append(messages, resp.ToParam())

    toolResults := runTools(ctx, resp.Content) // one result per tool_use block
    if resp.StopReason != anthropic.StopReasonToolUse {
        break
    }

    // All results from this turn go back in ONE user message.
    messages = append(messages, anthropic.NewUserMessage(toolResults...))
}

Plus the guardrails that make it survive production: tool errors returned as results with isError set so the model can recover, tool calls run concurrently through an errgroup with SetLimit, a hard MaxIterations (or a counter) and a context deadline over the whole loop, prompt caching so every turn doesn’t resend the full history at full price, and tests behind an interface so you can fake the model and assert on the loop instead of the prose.

That’s a good agent. It’s one agent, in one process, with one conversation in memory, doing whatever its tools allow, whenever someone calls it.

What you still have to build

Here’s the list I hit, roughly in the order it hurts.

1. Routing between bots

The second bot is where it stops being a loop. Now every message needs an answer to “who handles this?” The first version is always an @mention parser:

var mention = regexp.MustCompile(`@([A-Za-z][\w-]*)`)

func route(msg string, bots map[string]*Bot, lead *Bot) *Bot {
    for _, m := range mention.FindAllStringSubmatch(msg, -1) {
        if b, ok := bots[strings.ToLower(m[1])]; ok {
            return b
        }
    }
    return lead // nobody named: the lead decides
}

Then reality arrives. A message with two mentions. A message with none that clearly belongs to the reviewer. “ok go ahead”, which should go to whoever spoke last, not the lead. Then the bots start mentioning each other, and you’ve built a message bus with no loop protection: A hands to B, B hands back to A, and your MaxIterations only covers a single bot’s turn, not the chain between them. You need a hand-off budget across bots, duplicate detection, and a way to stop a teammate that keeps failing.

2. Approvals that aren’t a bool

In the tool-use post I wrote that anything that writes, sends, charges or deletes wants an approval gate: step the runner and confirm. That sentence hides a product. You need to decide what “risky” means (is git status risky? git push? curl localhost?), parse shell commands well enough to tell the difference, render the exact command to a human, wait for the answer without holding a goroutine and an HTTP request open for an hour, remember “always allow” and offer a way to take it back. And it has to fail closed. A regex that misses $(...) is worse than no gate at all, because you trust it.

3. Persistence

messages is a slice in memory. Restart the process and the agent has amnesia. So you add a database: conversations, tool calls and their results, pending approvals, which bot said what. Then memory that survives longer than one conversation, and the context-window problem that comes with it, because you can’t send six months of history on every turn. You end up writing summarisation and history windows, and you have to keep the cached prefix stable while you do it.

4. Schedules and routines

“Every weekday at 9, summarise what changed in the repo” sounds like a cron job calling your loop. Then you need the result to land in a chat someone reads, a run history, a “run now” button, a way to pause it, time zones, and a decision about what happens when a scheduled run needs an approval and nobody’s awake. The run waits, and someone has to see that it’s waiting.

5. Many accounts and engines

One API key is a demo. In practice you have a company Claude seat, a personal key, maybe Codex and a local model. When one runs out of usage, a turn should move to the next one instead of failing at 2 a.m. Some bots should stay on one account. Some work shouldn’t leave your machine at all.

6. Knowing when it’s actually done

The model says “Done, tests pass.” Did it run them? With one agent you read the transcript. With five agents working while you sleep, you need something that runs the check itself and keeps the receipt, otherwise “done” means “the model stopped talking”.

7. A UI for humans

None of the above is useful without a place to see it: chats, the approval cards, what’s waiting on you, what failed overnight. That’s a frontend, auth, and probably a phone.

Every item on that list is ordinary engineering. None of it is hard on its own. Together it’s months of work, and almost none of it is the interesting part: the tools and the prompts.

What a packaged team gives you

This is the gap kwant fills, and why I’ve been writing about it here. kwant is an open-source (AGPL-3.0), self-hosted team of AI bots: the same idea as Grok Bot, Claude Duet or OpenAI dots, but running on your own computer or server, with your own accounts. It’s written in Rust (Tokio, Axum, SQLite) with a React web app embedded in the binary, and it keeps its data in ~/.kwnt. I introduced it in Self-host a team of AI agents in one binary, and compared it with the hosted products and other open-source options in Open-source alternatives to Grok Bot, Claude Duet and Dots.

Mapped onto the list above:

You’d build What kwant ships
@mention parser and fallback routing One @mention goes to that bot; two or more go to the team lead, who passes parts on. With no mention, routing rules pick the bot (names, “Good for” / “Not for” lists, role words, what you taught it with Wrong bot?), close calls ask you, and a line under the reply says why that bot answered.
Bot-to-bot hand-offs Bots @mention each other to hand work on, and a team lead runs short discussions and posts one summary. Exact repeats and a teammate that failed 3 times in a row are skipped, with the reason.
Approval gate Per space or per bot: Ask before running commands, Ask only when risky (the default for new spaces) or Don’t ask. Risky steps such as git push, network calls, unknown commands or changes outside the bot’s folder show the exact command with Allow once, Always allow or Deny. The risky-command check is an allowlist that fails closed. Payments always ask, in every mode.
Persistence and memory Chats, approvals and settings in SQLite (built into the binary), stored in ~/.kwnt by default. Memory and skills per bot, and new skills wait for your approval.
Cron and run history Routines: once, daily, weekly, monthly, every few minutes or a custom cron rule, each with Run now, a history of its last 200 runs and an on/off switch. Bots can also set themselves one-off follow-ups.
Account juggling Claude, Codex or Grok CLI logins, Anthropic, OpenAI or xAI keys, Ollama, or your own OpenAI- or Anthropic-compatible server. Several accounts per engine with a priority order; when one runs out, the turn moves to the next and the chat says so. A bot can be pinned to one account.
“Is it really done?” A Mission board with one card per hand-off. A card turns Verified only when kwant runs the check itself, such as a test or a file check, and keeps the receipt.
A UI The web app, Mac and Linux apps, a Windows beta, and a phone app installed from the browser when kwant runs on a server you can reach over HTTPS. An Inbox shows what’s waiting on you, what finished and what failed.
Separate contexts Spaces: each with its own bots, teams, channels, routines, accounts and approval rule.

A few details I recognised from my own Go code. When a bot runs on an API key, kwant runs the tool loop itself and caps the model calls in a turn, the same idea as MaxIterations; a turn that hits the cap says so in its reply instead of silently stopping. When a step fails, the bot retries, then tries another way, and a step you declined isn’t tried again. If nothing works, its last line starts with Blocked: and says what would unblock it. That’s the “errors are results, not exceptions” rule from the tool-use post, extended to a whole team. There’s also an opt-in daily spending stop across all turns, which is the guardrail I always meant to add to my own loop and never did.

For CLI accounts it’s the other way round: kwant supervises the claude, codex or grok CLI you already have signed in, with their own file and shell tools, so you’re not paying for a separate API key on top of a subscription you already have.

Things kwant also has that I wouldn’t build myself, and won’t go into here: a shared browser for the bots, MCP tool servers per space, and a Dev mode that shows what landed in each team’s repo, with pushes to main and deploys always asking first. Flight School, where a bot earns more freedom per kind of work, is opt-in and only you grant it.

You don’t have to throw your Go code away

The part I like most as a Go developer: kwant also works the other way round. It runs an MCP endpoint, POST /api/mcp/agents, that coding agents and other MCP clients can call to ask your bots for work. Under Settings → All spaces → Coding agents you make a connection with its own kag_ token, limited to the spaces and bots you pick. The tools are list_bots, ask, get_reply and list_threads. For Claude Code, the setup is one line:

claude mcp add --transport http kwant https://<server>/api/mcp/agents \
  --header "Authorization: Bearer kag_…"

The limits are sensible. The agent can’t approve anything: approvals still come to you, and the agent only sees waiting_on_person. The token gives a 403 on every other kwant route. Each connection gets up to 60 asks an hour and 3 turns working at once, and you can revoke it immediately. Any MCP client can use the same endpoint, including one you write in Go. So “keep my Go service” and “run a team” aren’t either/or: your service can hand the open-ended, multi-step work to the team and get the reply back.

When to keep writing Go, when to run kwant

I still write agent loops in Go. Here’s how I decide.

Keep writing Go when:

  • The agent is a feature of your product. A support-ticket classifier, a code-review step in CI, an extraction pipeline. Your users never see a “bot”, they see a result. You want the loop inside your service, with your logging, your metrics, your tests. Everything in the testing post applies, and a separate app would just be another moving part.
  • The task is narrow and the tools are yours. Three tools, one model, a clear stop condition. The loop plus MaxIterations plus a deadline is the whole job.
  • You need tight control over every request. Exact cache breakpoints, a specific model per call, strict latency budgets. When you’re tuning the cached prefix byte by byte, you want to own the request.
  • Multi-tenant SaaS. If you’re serving many customers, you’re building a platform, and its shape is yours to design.

Run a packaged team when:

  • The “users” are you and your team, and the work is open-ended: research, triage, writing, chores on your repos, routines that run while you’re away.
  • You need more than one agent, and you’d otherwise be writing routing, hand-offs and hand-off budgets yourself.
  • You need a human in the loop with real approval cards, and payments that never go through silently.
  • It has to keep running on a schedule, on a VPS, with your laptop shut. (Self-host AI agents on a VPS covers that setup.)
  • You’d rather use the subscriptions you already pay for than wire up yet another API key.

The honest middle ground is both: your Go service owns its narrow, product-critical loop, and a kwant team does the open-ended work around it, reachable over MCP when the service needs it.

Caveats

To be fair about the trade-offs:

  • It’s your machine. Approvals guard against accidents; the docs say plainly that Ask only when risky isn’t a sandbox. Scripts in a bot’s folder can do anything the bot’s OS user can. Use Ask before running commands for bots or folders you don’t trust, or VM mode.
  • It’s opinionated. You get kwant’s routing rules and its UI. If you need a very different shape, writing your own is still the right call.
  • Your prompts still go to your providers. Self-hosted means the app and data are on your hardware. Only bots on local models keep prompts there too.
  • AGPL-3.0. Free to use, change and run for yourself, your team or your company. If you want to sell or host kwant without publishing your changes, there’s a commercial license.

Try it

If the list of things you still have to build looked familiar:

  1. Install kwant. The Mac app, the Linux .deb or AppImage (walkthrough in Install kwant on Linux), the Windows beta, or a server via curl -fsSL https://kwant.si/install.sh | sh or Docker Compose. Or, since you’re reading a Go blog and probably like building things yourself, from a clone (you’ll need Rust and Node.js 20+):

    git clone https://github.com/alexsergivan/kwant.git
    cd kwant
    make run   # builds the web app, then runs `cargo run -- serve`
    

    Then open http://127.0.0.1:8799. The first start takes a few minutes while the Laya router downloads its model.

  2. Add an account you already have, such as a Claude, ChatGPT or Grok login or an API key, from the Get started card.

  3. Make a small team and hand it a task with an @mention. Watch the hand-offs and the reason lines, and try a git push to see the approval card.

  4. Add a routine, and check the Inbox in the morning.

If it saves you from writing the routing layer, star kwant on GitHub. It’s open source, runs on hardware you control, and stars genuinely help other people find it. More at kwant.si.