Deploy an OpenAI Agents SDK agent to Blaxel as a serverless auto-scalable API and colocate it close to the sandboxes it works on.
The OpenAI Agents SDK is an open source, production-grade library for building agentic applications leveraging the Codex harness. One of the most interesting features of this SDK is the ability to create agents that are backed by remote execution environments (sandboxes) in the cloud. This ability allows the harness to get access to its own computer to execute commands, work with files and directories, write code, and perform computations.This tutorial explores how you can deploy your OpenAI Agents SDK projects to Blaxel with minimal code editing (and zero configuration), enabling you to colocate them close to the sandboxes the agents work on.
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.
The main way to deploy an agent on Blaxel is with the Blaxel CLI. This method is detailed in this tutorial. Alternatively you can connect a GitHub repository - any push to the main branch of the repository will automatically update the deployment on Blaxel - or deploy from a variety of pre-built templates using the Blaxel Console.Install the Blaxel CLI and log in to Blaxel using this command:
This creates a simple sandbox-backed agent with the OpenAI Agents SDK. Queries that you send the agent will be executed in a sandbox named agent-sandbox that is dynamically created and managed in your workspace on Blaxel’s infrastructure.
The agent’s HTTP service must be bound to the host and port provided by Blaxel. Blaxel automatically injects these values as HOST and PORT variables into the runtime environment. It is important to read these variables in your code and ensure that the agent’s HTTP service binds to the correct host and port.
Test the agent by making the endpoint available locally:
bl serve --hotreload &
This starts the agent locally while handling the core agent logic, function calls and model API calls exactly as they would be when deployed on Blaxel. The --hotreload flag monitors and reloads the agent if the source code changes.Note the host port on which the agent is running.In another terminal, send the agent a request (update the endpoint URL below with the correct port number for your agent):
curl -X POST http://0.0.0.0:8000/query \ -H "Content-Type: application/json" \ -d '{"task": "Install a complete Go development environment. Return the Go version and Go environment variables."}'
The agent will create and deploy a Blaxel sandbox using the 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.
You’re now ready to deploy the agent on Blaxel. When deploying to Blaxel, your workloads are served optimally to dramatically accelerate cold-start and latency while enforcing your deployment policies.First, create a requirements.txt file in the project directory with the following dependencies:
Then, deploy the agent by running the following command:
bl deploy
The Blaxel CLI will prompt for the type of resource you are deploying (“agent”). It will then auto-detect other details of your project and begin the deployment. The deployment process usually takes a few minutes, and you can watch progress from the Blaxel Console.Once the deployment process is complete, log in to the Blaxel Console to find the global endpoint for your agent service. Typically, this will be of the form https://run.blaxel.ai/WORKSPACE/agents/AGENT.
By default, agents deployed on Blaxel are not public. All agent requests must be authenticated via a bearer token. Requests can be made either via the Blaxel API or the Blaxel CLI.Test the deployed agent by sending an authenticated request to its global endpoint (update the endpoint URL below with the correct endpoint URL for your agent, and modify the task as desired):
curl -X POST https://run.blaxel.ai/$(bl workspace --current)/agents/sandbox-agent/query \ -H "X-Blaxel-Workspace: $(bl workspace --current)" \ -H "X-Blaxel-Authorization: Bearer $(bl token)" \ -H "Content-Type: application/json" \ -d '{"task": "Write a Python program to reverse words in a string. Test it. Return the test results and the final working code."}'
As before, the agent will create a sandbox, generate the code and provide the result.You can also send a request through the Blaxel CLI:
bl run agent sandbox-agent/query --data '{"task": "Write a Python program to reverse words in a string. Test it. Return the test results and the final working code."}'