cpuos

Integration · Agent framework

LangChain and LangGraph with cpuos sandboxes

Add a sandboxed code execution tool to LangChain and LangGraph agents, with the model on any OpenAI-compatible endpoint.

SDK
@cpuos/sdk · pip install cpuos
Model endpoint
https://gpuos.si/v1 or any OpenAI-compatible
Status
Early access

A LangChain tool that runs in cpuos

LangChain's create_agent builds a tool-calling agent on top of LangGraph. Any function decorated with @tool becomes a tool, so the sandbox is one function away.

Install
pip install langchain langchain-openai cpuos
agent.py
import osfrom cpuos import Sandboxfrom langchain.agents import create_agentfrom langchain_core.tools import toolfrom langchain_openai import ChatOpenAIllm = ChatOpenAI(    model="qwen3-32b",    base_url="https://gpuos.si/v1",    api_key=os.environ["GPUOS_API_KEY"],    temperature=0,)sbx = Sandbox.create(template="python", timeout="30m")@tooldef run_python(code: str) -> str:    """Run a Python 3 script in an isolated sandbox. Files live in /work.    Returns the exit code, stdout and stderr."""    sbx.files.write("/work/main.py", code)    run = sbx.exec("cd /work && python main.py", timeout="2m")    return f"exit_code={run.exit_code}\n{run.stdout[-4000:]}\n{run.stderr[-2000:]}"agent = create_agent(model=llm, tools=[run_python])result = agent.invoke(    {"messages": [{"role": "user", "content": "How many primes are below 1,000,000?"}]})print(result["messages"][-1].content)

The examples point the model at gpuOS, which serves open models on your own GPUs at https://gpuos.si/v1 with an OpenAI-compatible API. Any other OpenAI-compatible provider works the same way: change the base URL, the key and the model name.

cpuos is in early access: @cpuos/sdk (npm) and cpuos (PyPI) ship to early-access teams first, and read the API key from CPUOS_API_KEY. The calls on this page show the current API shape.

Sandboxes in a custom graph

In a hand-built LangGraph graph, keep the sandbox id in the graph state rather than the sandbox object. A node that needs to run code resumes the sandbox by id, runs the command and pauses it again. That way the state stays serializable for checkpointers, and a thread that resumes hours later gets its files back.

Node
from typing import TypedDictfrom cpuos import Sandboxclass State(TypedDict):    sandbox_id: str | None    code: str    output: strdef execute(state: State) -> dict:    if state["sandbox_id"]:        sbx = Sandbox.resume(state["sandbox_id"])    else:        sbx = Sandbox.create(template="python", timeout="1h")    sbx.files.write("/work/main.py", state["code"])    run = sbx.exec("cd /work && python main.py", timeout="2m")    return {"sandbox_id": sbx.pause(), "output": run.stdout[-4000:]}

Official documentation: langchain-ai.github.io/langgraph

Questions

Does this work with LangGraph's prebuilt agents?
Yes. A sandbox tool is a normal LangChain tool, so it works with create_agent, the older prebuilt ReAct agent and custom graphs.
Can the model be on gpuOS while the sandbox is on cpuos?
Yes. They are separate services: ChatOpenAI points at the gpuOS base URL, and the tool calls the cpuos SDK.
Is the Python REPL tool from langchain-experimental an alternative?
It runs code in your own process, with your credentials and network. That is fine for local experiments, not for code from users or untrusted inputs.

Related

Run LangChain and LangGraph tool calls in a sandbox

cpuos is in early access: a Firecracker microVM per task, hosted in the EU or on your servers, billed per second and free while paused.

gpuOS · where models think

Need the model too? Run it on gpuOS

gpuOS serves open models on your own GPUs behind one OpenAI-compatible API. The model reasons on gpuOS, the agent acts in a cpuOS sandbox.