LogoFreestyle

Langgraph JS

Code execution for Langgraph in JavaScript

Install the required dependencies

npm install freestyle-sandboxes @langchain/langgraph @langchain/core @langchain/openai

This walkthrough uses @langgraph/openai, however these exact steps should work for any of the langgraph providers like @langgraph/anthropic

Get your Freestyle API Key from the Freestyle Dashboard

Set up the Code Executor

import { executeTool } from "freestyle-sandboxes/langgraph";
 
const codeExecutor = executeTool({
  apiKey: process.env.FREESTYLE_API_KEY!,
});

You can also pass in any nodeModules, environment variables, timeout, or network restrictions you need.

import { executeTool } from "freestyle-sandboxes/langgraph";
 
const codeExecutor = executeTool({
  apiKey: process.env.FREESTYLE_API_KEY!,
  nodeModules: {
    mathjs: "14.3.1",
  },
  envVars: {
    MY_SUPER_SECRET_KEY: "1234567890",
  },
});

Set up the Langgraph SDK Agent

const model = new ChatOpenAI({ model: "gpt-4o" });
 
const agent = createReactAgent({
  llm: model,
  tools: [codeExecutor],
});

Invoke the Agent

const result = await agent.invoke({
  messages: [
    { role: "user", content: "What is the factorial of 13 divided by 55^2" },
  ],
});
console.log(result.messages.at(-1)?.content);

🚀 Your AI can now execute code

On this page