Build a coding agent with Modal Sandboxes and LangGraph

This example demonstrates how to build an LLM coding “agent” that can generate and execute Python code, using documentation from the web to inform its approach.

Naturally, we use the agent to generate code that runs language models.

The agent is built with LangGraph, a library for building directed graphs of computation popular with AI agent developers, and uses models from the OpenAI API.

Setup 

You will need two Modal Secrets to run this example: one to access the OpenAI API and another to access the LangSmith API for logging the agent’s behavior.

To create them, head to the Secrets dashboard, select “Create new secret”, and use the provided templates for OpenAI and LangSmith.

Creating a Sandbox 

We execute the agent’s code in a Modal Sandbox, which allows us to run arbitrary code in a safe environment. In this example, we will use the transformers library to generate text with a pre-trained model. Let’s create a Sandbox with the necessary dependencies.

We also need a way to run our code in the sandbox. For this, we’ll write a simple wrapper around the Modal Sandbox exec method. We use exec because it allows us to run code without spinning up a new container. And we can reuse the same container for multiple runs, preserving state.

Constructing the agent’s graph 

Now that we have the sandbox to execute code in, we can construct our agent’s graph. Our graph is defined in the edges and nodes modules associated with this example. Nodes are actions that change the state. Edges are transitions between nodes.

The idea is simple: we start at the node generate, which invokes the LLM to generate code based off documentation. The generated code is executed (in the sandbox) as part of an edge called check_code_execution and then the outputs are passed to the LLM for evaluation (the evaluate_execution node). If the LLM determines that the code has executed correctly — which might mean that the code raised an exception! — we pass along the decide_to_finish edge and finish.

We now set up the graph and compile it. See the src module for details on the content of the graph and the nodes we’ve defined.

Running the Graph 

Now let’s call the agent from the command line!

We define a local_entrypoint that runs locally and triggers execution on Modal.

You can invoke it by executing following command from a folder that contains the codelangchain directory from our examples repo:

If things are working properly, you should see output like the following: