Skip to main content
The Containers API allows you to create isolated execution environments with controlled network access, file systems, and resource limits for running code safely.

Create Container

Create a new container with optional files and skills:
container = client.containers.create(
  name: "data-processing",
  memory_limit: "2g",
  file_ids: ["file_abc123"],
  network_policy: {
    type: "allowlist",
    domains: ["api.example.com"]
  }
)

puts container.id
# => "container_xyz789"
name
string
required
Descriptive name for the container.
memory_limit
string
Memory limit for the container. Options: 512m, 1g, 2g, 4g. Default: 1g.
file_ids
array<string>
IDs of files to copy into the container at creation.
network_policy
object
Network access configuration:
  • { type: "disabled" } - No network access (default)
  • { type: "allowlist", domains: [...] } - Allow specific domains
skills
array
Skills to install in the container (by ID or inline definition).
expires_after
object
Expiration policy with anchor and seconds fields.

Retrieve Container

Get container details and status:
container = client.containers.retrieve("container_xyz789")

puts container.name
puts container.status
# => "ready"
id
string
Container identifier.
name
string
Container name.
status
string
Current status: initializing, ready, terminated, or failed.
created_at
integer
Unix timestamp of creation.
memory_limit
string
Configured memory limit.

List Containers

Retrieve all containers for your project:
containers = client.containers.list(
  limit: 20,
  order: "desc"
)

containers.auto_paging_each do |container|
  puts "#{container.name}: #{container.status}"
end
limit
integer
Number of containers to retrieve (default: 20, max: 100).
name
string
Filter by container name.
order
string
Sort order by creation time: asc or desc (default).
after
string
Cursor for pagination.

Delete Container

Permanently delete a container:
client.containers.delete("container_xyz789")

Container Files

Upload files to a running container:
require "pathname"

# Upload a file to the container
file = client.containers.files.create(
  "container_xyz789",
  file: Pathname("script.py"),
  path: "/workspace/script.py"
)

puts file.path
# => "/workspace/script.py"

List Container Files

List files in a container:
files = client.containers.files.list("container_xyz789")

files.each do |file|
  puts "#{file.path} (#{file.size} bytes)"
end

Retrieve File Content

Download a file from the container:
content = client.containers.files.content.retrieve(
  "container_xyz789",
  "file_abc123"
)

puts content.read

Delete Container File

Remove a file from the container:
client.containers.files.delete(
  "container_xyz789",
  "file_abc123"
)

Network Policies

container = client.containers.create(
  name: "isolated",
  network_policy: {
    type: "disabled"
  }
)
No outbound network access allowed.

Skills in Containers

Install skills for additional functionality:
container = client.containers.create(
  name: "with-skills",
  skills: [
    # Reference skill by ID
    { skill_id: "skill_abc123" },
    
    # Or define inline
    {
      type: "inline",
      source: {
        files: [
          {
            path: "helper.py",
            content: "def process(data): return data.upper()"
          }
        ]
      }
    }
  ]
)

Complete Example

require "openai"
require "pathname"

client = OpenAI::Client.new

# Create container with network access
container = client.containers.create(
  name: "data-processor",
  memory_limit: "2g",
  network_policy: {
    type: "allowlist",
    domains: ["api.weatherapi.com"]
  },
  file_ids: ["file_data_abc123"]
)

puts "Container created: #{container.id}"

# Upload processing script
script = client.containers.files.create(
  container.id,
  file: Pathname("process.py"),
  path: "/workspace/process.py"
)

puts "Script uploaded: #{script.path}"

# Use container in Responses API
response = client.responses.create(
  model: "gpt-4",
  input: "Process the uploaded data",
  tools: [{
    type: "code_interpreter",
    container: { container_id: container.id }
  }]
)

puts response.output.first.content

# Cleanup
client.containers.delete(container.id)

Resource Limits

Containers have the following resource limits:
  • Memory: 512 MB to 4 GB (configurable)
  • CPU: Shared, no dedicated cores
  • Disk: 10 GB ephemeral storage
  • Lifetime: 1 hour by default (configurable up to 24 hours)
  • Network: Controlled by network policy

Error Handling

begin
  container = client.containers.create(
    name: "test",
    memory_limit: "2g"
  )
rescue OpenAI::Errors::BadRequestError => e
  puts "Invalid request: #{e.message}"
rescue OpenAI::Errors::APIError => e
  puts "API error: #{e.message}"
end

Skills

Manage reusable skills

Files

Upload container files

Responses API

Use containers with code execution

Code Interpreter

Execute code in responses

Build docs developers (and LLMs) love