OpenAI Agents API sessions you start in your project with one call

Your app sends one REST call per OpenAI Agents API session, and Server4Agent runs its executor in your project until you stop it or the time limit ends.

By Nov 24, 2026Updated Nov 25, 20266 min read

A Server4Agent project can now be the self-hosted sandbox for sessions you run on the OpenAI Agents API. You connect an environment key to a project, your app tells Server4Agent when a session starts, and from then on that session's commands and file edits run in the project's folder, on your server.

Before this, an Agents API agent could call Server4Agent's MCP tools, but its shell ran in OpenAI's hosted sandbox or nowhere. What sets this setup apart from the Claude one is control: your app decides when each executor starts and how long it may run.

This post covers how to connect it, what your app has to do when a session starts, and what to know before you rely on it. Claude Managed Agents needs no call per session, because Server4Agent picks its sessions up by itself, so it has its own post.

Quick answer

Create an environment key on the Agents tab of the OpenAI dashboard. On the project's Settings tab in Server4Agent, open Advanced, choose Connect under Managed agents, pick the OpenAI Agents API and paste the key. Then, each time your app creates a session with a self-hosted environment, send the session's environment id and remote URL to Server4Agent, with one REST call or the agent_session MCP tool. Server4Agent wakes the project's server and starts the session's executor in the project's folder. It runs until your app stops it or a time limit you set runs out.

Key takeaways

  • A project can act as the self-hosted sandbox for OpenAI Agents API sessions.
  • Files the agent writes land in the project you deploy from, and the next session finds them there.
  • You connect the key on the dashboard, over REST, or with the connect_agent_environment MCP tool.
  • Your app starts each session's executor, because only your app sees a session created.
  • The environment key is stored encrypted, the same way project secrets are, and is never shown again. Admin keys are refused.
  • The server does not have to stay on between sessions. It wakes when your app starts one.

Why run the sandbox on your own project

The Agents API lets you choose where an agent's tools run. OpenAI's hosted sandbox is one option. A self-hosted environment is another: OpenAI keeps running the model, and a process you run, the executor, picks up the session's tool calls and runs them on your machine.

On Server4Agent the executor runs inside a project, so the folder the agent works in is the folder you deploy from. Because your app starts every executor, it can send one session after another to the same project, and each picks up the files the last one left.

Connect the OpenAI Agents API

  1. On the Agents tab of the OpenAI dashboard, create an environment key and set every other permission to None. Keep it apart from the API key your app uses to call the model.
  2. In Server4Agent, open the project's Settings tab, open Advanced, and choose Connect under Managed agents. Pick the OpenAI Agents API and paste the key. No environment id is needed here, because every session brings its own.

A project's connection is the key alone, so one connection serves every session your app sends to that project.

Start a session's executor from your app

In OpenAI's self-hosted setup, each session gets its own environment id and remote URL, and needs its own executor. Create the session the way OpenAI's guide describes, with an environment of type self_hosted. Then pass those two values to Server4Agent:

text
curl -X POST https://api.server4agent.com/projects/$PROJECT_ID/agent-sessions \
  -H "Authorization: Bearer $SERVER4AGENT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"environment_id": "<session environment id>", "remote_url": "<session remote url>", "max_minutes": 60}'

In Python, right after the session is created:

text
import os, requests

# session was created with a self_hosted environment, as in OpenAI's guide
r = requests.post(
    f"https://api.server4agent.com/projects/{os.environ['PROJECT_ID']}/agent-sessions",
    headers={"Authorization": f"Bearer {os.environ['SERVER4AGENT_API_KEY']}"},
    json={
        "environment_id": session.environment.id,
        "remote_url": session.environment.remote_url,
        "max_minutes": 60,
    },
)
r.raise_for_status()

Server4Agent answers at once and does the rest in the background. If the project's server is asleep, it wakes it. The first time, it installs OpenAI's executor on the server. Then it runs the executor in the project's folder, connected to that session, and the agent's tool calls start arriving there.

An agent connected to Server4Agent over MCP can do the same with the agent_session tool, with action set to start.

Stop it when the session is done

The executor runs until your app stops it with a DELETE on the same path, or until max_minutes runs out. That is 60 minutes by default and at most 360.

text
curl -X DELETE "https://api.server4agent.com/projects/$PROJECT_ID/agent-sessions?environment_id=<session environment id>" \
  -H "Authorization: Bearer $SERVER4AGENT_API_KEY"

Leave out environment_id to stop every executor running in the project. Once nothing is running, the server can go back to sleep.

What happens to your key

Anyone holding the OpenAI environment key could attach an executor to your sessions, so Server4Agent guards it closely:

  • It is encrypted at rest and no dashboard page, API response or MCP tool ever returns it. The dashboard shows only its last four characters.
  • Only the worker that runs sessions opens it. It hands the key to the executor through a private file on the server, which is deleted before the executor starts, never on a command line.
  • Admin keys are refused. Paste the environment key you made for this, not a key that can manage your OpenAI organization.
  • The remote URL decides where the key is sent, so Server4Agent only accepts a wss or https address on an openai.com or chatgpt.com host, and refuses anything else before the key leaves the store.

Disconnect, in the same Advanced section, or the disconnect_agent_environment tool ends it: every executor still running in the project is stopped and the key is deleted.

What to know before you rely on it

  • Your app starts every session. OpenAI has no way to tell Server4Agent that a session has started, so nothing runs until your app makes the call above.
  • The executor does not stop on its own when it goes quiet. Stop it from your app, or set max_minutes to how long a session should be allowed to run.
  • OpenAI's executor is an alpha release. OpenAI publishes it on its alpha tag, so its behaviour may change with them.
  • One OpenAI connection per project. Connecting again replaces the key. The same project can also have one Claude Managed Agents connection.
  • Start calls can queue. Each server and the service as a whole run a limited number of executors at once, so a burst of start calls can wait its turn.
  • A paused server refuses start calls. While the project's server is paused, your start call fails until you resume it.

FAQ

What is a self-hosted sandbox for the OpenAI Agents API?

It means OpenAI keeps running the model while the agent's tool calls, like shell commands and file edits, run on a machine you pick instead of OpenAI's hosted sandbox. An executor you run receives each call and returns the result.

Is this the same as the OpenAI Agents SDK?

No. The Agents SDK is OpenAI's library for running an agent loop in your own code. This post is about the Agents API, where OpenAI runs the session and sends its tool calls to an environment you host. Server4Agent is that environment.

Why does my app have to start each session?

Each Agents API session gets its own environment id and remote URL, and only the app that created the session sees them. Server4Agent needs both to connect the executor, so your app passes them on.

Does my Server4Agent server have to stay on?

No. It can sleep between sessions. Server4Agent wakes it when your app starts a session, and it can sleep again once the executor has stopped.

Do the agent's files survive between sessions?

Yes. The executor works in the project's folder, so whatever one session writes is still there when your app starts the next one, and you can deploy the project at its URL at any point.

Is my environment key safe?

Yes. It is encrypted, never displayed after you paste it, and leaves the server only toward an OpenAI address that your session gave.

External references

Questions, or a tool you want built?

Ask us about this post, or tell us what you would hand to an assistant first. A person reads every message and answers within one business day.