Back to all posts

How we built Code Mode

Building Code Mode for a SaaS agent that also runs locally comes with some subtle differences.

Joshua Segaran · · 5 min read

In part one, we discussed why Indent’s agent calls tools by writing code. In this post, we’ll talk about how we built our Code Mode.

Because Indent runs both in the cloud and locally, we faced some subtle challenges around latency, speed (especially for tool search and outbound connections), and deployment. (When we update our Code Mode to support new tools, changes to prompting, or improved tool search, we need to update our entire cloud fleet.)

Architecture

Scripts run on the agent's computer, which never holds integration credentials.

Our Code Mode implementation has three main components: a host on the agent’s computer (a cloud sandbox or the user’s Mac), the versioned runtimes it manages, and an integration proxy that runs on our servers.

The integration proxy manages access control to integrations without putting any credentials on the box, and sends the box the tool catalog its types are built from. When access changes or someone connects a new integration, the proxy pushes the new tool catalog right away. The integration proxy also lets us centralize audit, observability, and other controls on tool use.

The host is responsible for managing runtime upgrades, starting a runtime for each session, and maintaining connections to the integration proxy. The host also warms up the TypeScript compiler and its type cache (both shared across runtimes) to reduce latency once the model starts writing a script. The host also manages auto-updates for newly deployed versions of the runtime.

When our agent loop emits a tool call, the Code Mode script gets sent to the runtime. The runtime type-checks the TypeScript (in about 20 ms at the median), starts up a fresh QuickJS guest in parallel, runs the script in the guest, as well as any needed tool searches, and handles input/output for the QuickJS guest (including formatting the result for the agent).

Building a fast integration proxy

The integration proxy lets us avoid putting any credentials on the sandbox while still giving Code Mode scripts access to external services. We’ve put a lot of effort into making this fast.

Each session gets its own connection to the integration proxy, over a websocket managed by the host, as soon as the session starts. We inject a JWT into the host, which it then uses to identify itself. Our backend refreshes the JWT, and the host hands the new one to the proxy over the open websocket. The integration proxy caches each script’s access policy in memory, so it can do fast authorization checks before sending each request to the upstream server.

The integration proxy tells the runtime which integrations are connected, so it can set up tool search and type-check the integration types ahead of time (before any scripts are even written).

We maintain connection pools in the integration proxy for recently used MCP servers. The proxy opens a pooled session for an MCP server as soon as the first call for it arrives. That ends up saving about 400 ms (at the median) on 90% of MCP calls, because a single script can make hundreds of tool calls, many of which are to the same MCP servers. So connection pools bring our total execution time down significantly.

Using QuickJS as a sandbox

A script can only call what the runtime hands it, and the runtime can always kill it.

We don’t want to run untrusted code within our infrastructure. We chose QuickJS to sandbox Code Mode scripts because it’s a small engine that still gave us the isolation we needed. A script can only reach the functions we hand it. We also wanted to be able to set strict timeouts, limit memory usage, and control which side effects a script can have.

We also built jss, a wrapper that runs quickjs-ng in WebAssembly in a worker thread. Since the process that runs on our sandboxes and Macs (and our CLI) is Node, this let us avoid shelling out to a new process. jss is open source, and we encourage you to try it out: github.com/indent-com/jss.

Each script gets a fresh guest, which we start as soon as the script arrives so its startup overlaps the type check. We set deadlines both inside and outside the QuickJS sandbox: QuickJS checks the deadline as it runs, and if a script still doesn’t stop, we kill its worker. Output from text() is buffered inside the guest and sent along with the next tool call, so we don’t pay a serialization round trip for every line a script prints. When something throws, we use source maps to point the error back at the line in the TypeScript the agent wrote.

Most sessions that run Code Mode (about three quarters) use tool search to get the full arguments of the functions they call. We need search to be fast when the tool schemas change, as well as fast for each request.

We represent available tools as a catalog, which we turn into TypeScript interfaces for those tools. We deduplicate these interfaces to expose all of the methods defined on each interface to tool search. So, for example, a personal Gmail integration and an organization Gmail integration will share a single gmail.search entry in the search cache – we only have to look up the interface once, because it is shared across installations. When the catalog changes, the next search rebuilds the cache in a shared worker pool, so nothing blocks the tool calls themselves.

Catalog
hash 6a00
Search cache
Script
Gmail
personal
Gmail
organization
Linear
organization
Gmail
support
gmail.send
gmail.search
linear.list_issues
linear.save_issue
searchTools("search email")
→ gmail.search, 2 accounts
searchTools("search email")
→ gmail.search, 3 accounts
Each method is indexed once, so both Gmail accounts share one gmail.search entry.

Ranking is according to BM25F, which we precompute. Tool names have the highest weight, followed by specific keywords, then descriptions and parameter text. Each page of results holds up to 64 KiB, including every type a method references so the result type-checks, and follow-up pages skip types already printed. If a query word matches an installation’s name, search filters to that installation before ranking, and words that match nothing in the catalog come back as a comment, so the agent knows to connect an integration instead of rephrasing.

Hotswapping the runtime

Our host layer changes infrequently, but we update the runtime bundle many times throughout the day, whenever we deploy our backend. When we deploy, we generate a new runtime bundle and push its SHA down to the box. The host checks the SHA of these bundles, and whenever it pulls an update, it drains any scripts that are already running, allowing them to finish on the version they started with.

We also need to roll both sides — sandboxes and backend servers — while cleanly maintaining websocket connections. When we deploy our backend, each server closes its websockets as “going away” before it exits. The host treats these websocket closures as transient, opens a new websocket on its next request, and moves its in-flight scripts over to it.

Details, details, details

Getting a rudimentary Code Mode up and running is straightforward, but getting it fast, continually deployable, and working across a fleet of local and cloud computers involves a lot of nitty-gritty details. We think it’s important to be public about the real engineering challenges involved in shipping reliable enterprise agents. If this is something you’re working on too, we’d love to hear from you.

Start building today

Start on your Mac. From the first task, you're building the coworker your team will use.