What You’ll Learn
- Defining a
@toolwithlangchain_core.toolsthat executes Python in a Declaw sandbox - Creating a LangGraph ReAct agent with
create_react_agent - Demo mode that exercises the sandbox tool directly without needing an OpenAI key
Prerequisites
- Declaw instance running and
DECLAW_API_KEY/DECLAW_DOMAINset OPENAI_API_KEY(optional — the example runs in demo mode without it)
This example is available in Python. TypeScript support coming soon.
Code Walkthrough
1. Define the Declaw sandbox tool
WrapSandbox.create() in a LangChain @tool. The agent receives the function’s docstring as the tool description, so make it clear:
execute_python spins up a fresh sandbox, runs the code, and destroys the sandbox. Sandboxes are fully isolated — code from one call cannot affect another.
2. Create a LangGraph ReAct agent
3. Run the agent
execute_python with the generated code, receive the sandbox output, and formulate a final answer.