> ## Documentation Index
> Fetch the complete documentation index at: https://modal-computer-use.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Run an OpenAI computer loop

> Connect the OpenAI Responses API computer tool to a placed Modal desktop.

Your application owns the OpenAI client, conversation, limits, and approval policy. `OpenAIAdapter` only converts provider actions to SDK actions.

## Install the provider extra

```bash theme={"system"}
uv add "modal-computer-use[modal,openai]==2.0.2"
```

Store `OPENAI_API_KEY` in the placed Function. Keep it out of the desktop Sandbox.

## Keep the loop inside the placed Function

Follow the [session handoff](/build/session-handoff) guide. Create the desktop once, pass its
`ComputerSessionHandle` to a Function that declares the same region selector, and borrow once
around the full OpenAI loop.

```python theme={"system"}
async with handle.borrow_async(
    run_id=run_id,
    function_region=REGION,
) as computer:
    response = await run_openai_computer_loop(
        client=openai_client,
        computer=computer,
        task=task,
    )
```

Provider imports and loop state stay in your application.

## Preflight the complete response

OpenAI can return more than one `computer_call`. Normalize and validate every call before the first desktop mutation. Enforce limits for:

* Model turns
* Total trajectory actions
* Expanded actions in one call
* Action, batch, and loop time
* Actions that need user approval

Stop before mutation when the response exceeds a limit.

## Execute one step per computer call

Keep provider order. Send the complete ordered action array in one `computer.step()` call.

```python theme={"system"}
step = await computer.step(
    normalized_actions,
    continue_on_error=False,
    call_id=call.call_id,
    max_action_timeout_ms=30_000,
)

output = openai_computer_call_output(
    step.screenshot,
    call_id=call.call_id,
    detail="original",
)
```

`computer.step()` executes the batch once and returns its immediate screenshot. Your application
defines readiness and task success. Never replay a call after an unknown mutation outcome.

## Use the maintained example

The [OpenAI loop example](https://github.com/ashtonchew/modal-computer-use/blob/b60c1cb7495200e36a738c0f6e07961b1d2db93c/examples/03_openai_computer_loop.py) contains the placed Function, response-wide preflight, limits, batching, result conversion, cancellation, and cleanup.

<Warning>
  Treat tasks, page content, screenshots, provider output, typed text, and clipboard text as untrusted. Ask for approval before an action creates an external effect. Hand password changes and CAPTCHA handling to the user.
</Warning>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.