Prerequisites
- An OpenAI API key. If not, sign up for an OpenAI account and obtain an API key.
- A Blaxel account. If not, sign up for a Blaxel account.
- A Blaxel workspace and API key. Learn about Blaxel workspaces and how to obtain a Blaxel API key.
The OpenAI Agents SDK is provider-agnostic and can be used with both OpenAI and non-OpenAI models. This tutorial uses OpenAI models, but you can also read about integrating other models.
1. Install the Blaxel CLI and log in to Blaxel
Install the Blaxel CLI and log in to Blaxel using this command:2. Install required dependencies
Create a directory for the project:3. Configure the environment
Add your OpenAI API key, Blaxel API key and Blaxel workspace name as environment variables:4. Build a simple agent
In your project directory, create a file namedmain.py with the following code.
agent-sandbox-01 that is dynamically created and managed in your workspace on Blaxel’s infrastructure.
Run the agent:
blaxel/base-image image, then investigate the sandbox and install all the tools required for Go development. Once done, it will return a report of its work and automatically delete the sandbox. Here’s a partial example of the output:
5. Build a data analysis agent
In addition to running commands, agents can also create, delete and manage files and directories in the sandbox filesystem. Generated files can be transferred from the sandbox environment to the host (and vice-versa). To see this in action, update the simple agent from the previous example to perform data processing and analysis in a sandbox:blaxel/py-app image to create a sandbox containing a complete Python development environment. When the sandbox is created, the SDK automatically also creates a new workspace directory (defaults to /workspace but can be configured to point to another location) for the agent to operate in.
The agent then uses sandbox tools to call the remote API, obtains a dataset (mock data), and autonomously installs all the required packages for chart creation. It then prepares and saves a chart of the received data to the workspace at /workspace/chart.png. This file is then read from the sandbox session and downloaded to the local host. Once the session is complete, the sandbox is terminated.
Run the agent - once complete, you will have a new chart.png file in your project directory containing the generated chart:
6. Build a coding agent
One of the most popular uses for agents is to generate code. Sandboxes provide isolated execution environments, allowing agents to securely run generated code with no risk of escaping. Blaxel sandboxes offer a unique advantage here: human operators can preview agent-generated applications in real-time via direct preview URLs for each running sandbox. To see this in practice, update the agent code inmain.py as below:
blaxel/nextjs image, which contains a complete Next.js development environment and a Next.js skeleton application. Notice the ttl parameter, which sets an expiration policy to automatically delete the sandbox after 30 minutes.
In addition, the exposed_port_public parameter and the resolve_exposed_port() function call take care of producing a preview URL for the user, and the pause_on_exit parameter prevents the sandbox from being automatically deleted at the end of the session.
Test the agent:
Resources
Want more info on developing and deploying agents on Blaxel? Check out the following resources:Run OpenAI Agents SDK agents on Blaxel
You don’t have to stop at the sandboxes! Deploy OpenAI Agents SDK on Blaxel.
Deploy your agent code to Blaxel
Complete tutorial for deploying AI agents to Blaxel.
Manage environment variables
Complete tutorial for managing variables and secrets when deploying to Blaxel.
