---
title: "Building with Modal and the OpenAI Agents SDK"
description: "The post walks through constructing a custom agent harness with the OpenAI Agents SDK and Modal Sandboxes, starting from a minimal coding agent that executes shell commands directly on the host. To improve safety, the agent is moved into isolated Modal Sandboxes, which are secure Linux environments that can be equipped with GPUs for compute-intensive tasks like training models. Memory is added via Sessions to accumulate context across turns, and subagents are introduced to handle focused subtasks with fresh context windows, preventing context bloat in the orchestrator while allowing long-horizon work. An async subagent pool enables parallel execution, with quotas to limit GPU spend, and filesystem snapshots allow checkpointing and deduplication of setup work across subagents, saving time and offloading memory. The harness is applied to the Parameter Golf challenge, where an orchestrator delegates parallel training experiments across different ML frameworks on GPUs, demonstrating autonomous research capabilities with composable skills."
---

# Building with Modal and the OpenAI Agents SDK

[Modal](https://yomu.fyi/company/modal) · Apr 15, 2026

**Type:** Tutorial

## Summary

The post walks through constructing a custom agent harness with the OpenAI Agents SDK and Modal Sandboxes, starting from a minimal coding agent that executes shell commands directly on the host. To improve safety, the agent is moved into isolated Modal Sandboxes, which are secure Linux environments that can be equipped with GPUs for compute-intensive tasks like training models. Memory is added via Sessions to accumulate context across turns, and subagents are introduced to handle focused subtasks with fresh context windows, preventing context bloat in the orchestrator while allowing long-horizon work. An async subagent pool enables parallel execution, with quotas to limit GPU spend, and filesystem snapshots allow checkpointing and deduplication of setup work across subagents, saving time and offloading memory. The harness is applied to the Parameter Golf challenge, where an orchestrator delegates parallel training experiments across different ML frameworks on GPUs, demonstrating autonomous research capabilities with composable skills.

## Context

The post addresses the need for customizable internal agent systems, inspired by companies like Ramp building background coding agents on Modal. It uses the OpenAI Agents SDK and Modal Sandboxes to create a secure, scalable harness for autonomous coding and research tasks, specifically applied to the Parameter Golf challenge of minimizing parameters while maintaining intelligence.

## Approach / What changed

Build a custom agent harness by starting with a basic Agent, moving execution into Modal Sandboxes for safety, adding Sessions for memory, introducing subagents for context delegation, implementing an async subagent pool with quotas, using filesystem snapshots for checkpointing, and adding a skills subsystem for task-specific prompts.

## Takeaways

- Moving agent execution into Modal Sandboxes isolates the LLM from the host, preventing security risks from arbitrary shell commands while allowing GPU access for compute tasks.
- Using subagents with fresh context windows prevents context bloat in the orchestrator, enabling long-horizon tasks by delegating focused work to short-lived agents.
- Filesystem snapshots allow checkpointing sandbox state, enabling subagents to start from a known state and avoid redundant setup work, saving GPU time and offloading memory.

## FAQ

### How does the OpenAI Agents SDK integrate with Modal Sandboxes?

The SDK provides a SandboxAgent class and ShellTool that connect to a ModalSandboxSession, allowing agents to run commands in isolated Linux environments. ModalSandboxClientOptions can request GPUs for the sandbox.

### How does the harness prevent context bloat in long-running agents?

The harness uses an orchestrator agent with a fresh context window for each subagent invocation. Subagents handle focused tasks and return summaries, while the orchestrator maintains only high-level context. Sessions accumulate memory across turns but subagent sessions are scrapped after task completion.

### How does the harness parallelize tasks and control GPU costs?

An async SubAgentPool manages multiple parallel subagents, each running in its own sandbox. The orchestrator can spawn subagents and selectively wait for results. A quota system limits the number of expensive GPU subagents (e.g., 8x H100s) that can run concurrently.

**Tags:** [AI Agents](https://yomu.fyi/topic/ai-agents), [GPU](https://yomu.fyi/topic/gpu), [Modal](https://yomu.fyi/topic/modal), [Parallelism](https://yomu.fyi/topic/parallelism), [Sandbox](https://yomu.fyi/topic/sandbox)

- Source: [Modal](https://modal.com/blog/building-with-modal-and-the-openai-agent-sdk)
- Source URL: https://modal.com/blog/building-with-modal-and-the-openai-agent-sdk
- Ingested by Yomu: 2026-09-20T07:46:44.061Z

[Read original post](https://modal.com/blog/building-with-modal-and-the-openai-agent-sdk)
