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.
pip install langchain langchain-openai cpuosimport 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.
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