Skip to main content
Serverless Sandboxes is in public preview.
In this tutorial, you invoke an agent in a W&B Serverless Sandbox. You start a sandbox with the required environment variables, install dependencies, and run a Python script that creates and invokes a simple OpenAI agent. The agent uses tool calls to get weather for a location and return a punny forecast.
This tutorial uses OpenAI as the language model for the agent, which requires an OpenAI API key.

Prerequisites

Before you begin, install the W&B Python SDK, authenticate with W&B, and store your OpenAI API key as a secret so the sandbox can use it.

Install W&B Python SDK

Install the W&B Python SDK using pip:

Log in and authenticate with W&B

Run the wandb login CLI command and follow the prompts to log in to your W&B account:

Store API keys in Secret Manager

Store your OpenAI API key in the W&B Secret Manager as OPENAI_API_KEY. For more information about using secrets in sandboxes, see Secrets. This lets you securely access the API key in the sandbox without hardcoding it in your code or sandbox configuration.

Copy agent code

With your prerequisites in place, save the agent code locally so you can run it inside a sandbox. The script defines an OpenAI agent with two tools and a structured response format.
Copy the following code into a file named demo.py in the same directory as this tutorial. Then, run the previous code snippet to invoke an OpenAI agent in a sandbox.
demo.py