A Multi-Tenant Coding-Agent Runtime in 2,000 Lines of Python
Contents Jump to a section
This post predates my CLI coding-agent deep dive. Building a runtime from scratch is how I first learned what an agent framework is made of. The main lesson still holds: the agent loop is easy; isolation, persistence and cost are where the engineering is.
The problem
I wanted a SaaS where users describe a feature in plain English and an AI agent writes the code. That needs three things at once:
- Isolation. User A’s code must never leak to user B.
- Persistence. Work has to survive between sessions.
- Security. Untrusted code must not run dangerous commands on the production servers.
I could not find a library that combined coding tools, isolated execution and session persistence, so I wrote one: omniagents, about 2,000 lines of Python.
What is in it
Ten coding tools, inspired by Gemini CLI. They are plain classes, with no dependency on LangChain or smolagents:
from omniagents.tools.write_file_tool import WriteFileTool
from omniagents.tools.read_file_tool import ReadFileTool
from omniagents.tools.run_shell_command_tool import RunShellCommandTool
from omniagents.tools.glob_tool import GlobTool
from omniagents.tools.search_file_content_tool import SearchFileContentTool
from omniagents.tools.replace_tool import ReplaceTool
# + 4 more
Three execution backends. The tools stay the same, only the environment changes, so you can swap backends without touching the agent:
from omniagents.backends.local_backend import LocalBackend # Your machine
from omniagents.backends.docker_backend import DockerBackend # Isolated container
from omniagents.backends.e2b_backend import E2BBackend # Cloud sandbox
Three state managers, which decide where a user’s workspace is saved:
from omniagents.backends.state_manager import (
NoOpStateManager, # No persistence
GitStateManager, # Save to GitHub branches
GCSStateManager, # Save to Google Cloud Storage
)
Each layer can be replaced on its own:
+------------------------------------------+
| Your LLM Framework |
| (LangChain, Pydantic-AI, smolagents) |
+--------------------+---------------------+
|
+--------------------v---------------------+
| 10 Core Tools |
| (framework-agnostic, just functions) |
+--------------------+---------------------+
|
+--------------------v---------------------+
| Execution Backend |
| (Local, Docker, or E2B) |
+--------------------+---------------------+
|
+--------------------v---------------------+
| State Manager |
| (Git, GCS, or nothing) |
+------------------------------------------+
One backend per user
Multi-tenancy comes down to one backend per user, identified by a project_id:
def get_agent_for_user(user_id: str):
backend = DockerBackend(
project_id=f"user-{user_id}", # Unique per user
state_manager=GCSStateManager(), # Persists to cloud
)
backend.start() # Loads previous state if exists
return LangChainAgent(
backend=backend,
model=ChatOpenAI(model="gpt-4"),
preset=PythonUVPreset(),
)
# Alice gets her own container + persistent storage
alice = get_agent_for_user("alice")
alice.run("Create a Flask app")
alice.backend.shutdown() # Saves to GCS
# Bob is completely isolated
bob = get_agent_for_user("bob")
bob.run("Build a CLI tool")
bob.backend.shutdown()
# Alice returns tomorrow -- her Flask app is still there
alice = get_agent_for_user("alice")
alice.run("Add authentication")
The same tools plug into any framework. Each tool has to_langchain_tool(),
to_pydantic_ai_tool() and to_smolagents_tool():
agent = LangChainAgent(backend=backend, model=model, preset=preset)
agent = PydanticAIAgent(backend=backend, model=model, preset=preset)
agent = SmolagentsAgent(backend=backend, model=model, preset=preset)
What I learned
A few tools go a long way
Every coding agent uses roughly the same core tools: read a file, write a file, run a command. The difference between agents is in the details. The better ones add web browsing and search, a task tracker (plan mode), a think tool for hard reasoning, and a delegate tool that starts sub-agents.
Frameworks help you start, not finish
smolagents, LangChain and Pydantic-AI are good for a prototype. They get in the way as soon as you need to customize the loop. A basic agent loop is about 50 lines, and once you own it, customizing it is easy. My advice: validate the idea with a framework, then write the core loop yourself.
Where the money goes
Proprietary models (GPT-4, Claude at the time) add up fast. Open models served through Hugging Face providers such as Groq and Cerebras had better prices and rate limits.
For compute, E2B handles everything but is expensive and Python-only. Docker on an EC2 machine gives predictable pricing and full control, but you have to worry about running untrusted code. Fly.io and Modal scale per request at a mid-range cost. For a side project I would start with local Docker; for production I would look at Fly.io.
Persistence matters more than expected
A real product needs saved state to resume work, debug what happened, and bill for what was produced. Git storage is free and versioned, which works for small projects. Use GCS for larger files.
OpenHands solves a different problem
OpenHands is an application: a full product you deploy. Omniagents is a library: primitives you compose. They have different goals, and both are useful.
Try it
uv add git+https://github.com/charles-azam/omniagents.git
from omniagents.backends.local_backend import LocalBackend
from omniagents.backends.state_manager import NoOpStateManager
from omniagents.tools.write_file_tool import WriteFileTool
from omniagents.tools.run_shell_command_tool import RunShellCommandTool
backend = LocalBackend(project_id="demo", state_manager=NoOpStateManager())
backend.start()
write = WriteFileTool(backend=backend)
write.execute(absolute_path="hello.py", content="print('Hello from Omniagents!')")
shell = RunShellCommandTool(backend=backend)
result = shell.execute(command="python hello.py")
print(result.content) # "Hello from Omniagents!"
backend.shutdown()
When I wrote this, the next steps were a Fly.io backend and an MCP server interface for Claude Desktop.
Links: omniagents on GitHub, its documentation, and OpenHands.