How to trace agent activity inside a LangSmith sandbox

Last updated: April 22, 2026

Solution

Pass LangSmith tracing environment variables via the env parameter on sandbox.run(). Your code inside the sandbox must call flush() before exiting to ensure traces are sent.

1. Script running inside the sandbox (my_agent.py)

import langsmith
from langsmith import traceable

@traceable(name="my-sandbox-task")
def do_work():
    return "Hello from the sandbox!"

result = do_work()
print(result)

# Required: flush traces before the process exits
langsmith.Client().flush()

2. Host script that launches the sandbox

from langsmith.sandbox import SandboxClient

client = SandboxClient()

tracing_env = {
    "LANGSMITH_API_KEY": "lsv2_pt_...",
    "LANGSMITH_ENDPOINT": "https://api.smith.langchain.com",
    "LANGSMITH_TRACING": "true",
    "LANGSMITH_PROJECT": "my-sandbox-traces",
}

with client.sandbox(template_name="my-template") as sandbox:
    sandbox.run("pip install langsmith", timeout=120, env=tracing_env)
    result = sandbox.run("python3 my_agent.py", env=tracing_env)
    print(result.stdout)

How it works

  • Sandboxes have outbound network access — the LangSmith SDK inside the sandbox can reach the API directly

  • The env parameter sets environment variables for that specific command execution

  • Any LangSmith-instrumented code (@traceable, LangChain, LangGraph) will automatically pick up the tracing config

  • flush() blocks until all queued traces are delivered to the server. There may be a short indexing delay (~10-15s) before traces appear in the UI, but they will land

  • Also available here