> ## 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 Anthropic computer loop

> Connect the Anthropic Messages API computer tool to a placed Modal desktop.

Your application owns the Anthropic client, message history, limits, and approval policy. `AnthropicAdapter` only converts provider actions to SDK actions.

## Install the provider extra

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

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

## Select matching tool versions

| Tool | Beta header | Use |
| - | - | - |
| `computer_20251124` | `computer-use-2025-11-24` | Current integration with optional zoom |
| `computer_20250124` | `computer-use-2025-01-24` | Compatible older integration |
| `computer_20241022` | `computer-use-2024-10-22` | Adapter compatibility |

The adapter defaults to `computer_20251124`. Set `enable_zoom=True` when you want zoom with that version.

## Keep the loop inside the placed Function

Follow the [session handoff](/build/session-handoff) guide. Borrow once around the complete Messages API loop.

```python theme={"system"}
async with handle.borrow_async(
    run_id=run_id,
    function_region=REGION,
) as computer:
    response = await run_anthropic_computer_loop(
        client=anthropic_client,
        computer=computer,
        task=task,
        display_width_px=1280,
        display_height_px=800,
    )
```

Keep the complete assistant content in message history. Continue only when `stop_reason` is `tool_use`. Return all matching tool results in the next user message.

## Preserve hosted-tool order

Execute hosted computer calls in response order. Use `computer.step()` for actions that need an immediate screenshot.

```python theme={"system"}
step = await computer.step(
    [normalized_action],
    continue_on_error=False,
    call_id=tool_use.id,
)
```

Keep these provider semantics:

* Return cursor coordinates as text for `cursor_position`.
* Reuse the native image from `screenshot` or `zoom` when present.
* Avoid a duplicate trailing image after a native image action.
* Keep separate hosted calls separate.

Use a custom batch tool only for a fixed sequence that needs no intermediate model decision. Send its ordered actions in one `computer.step()` call.

## Use the maintained example

The [Anthropic loop example](https://github.com/ashtonchew/modal-computer-use/blob/b60c1cb7495200e36a738c0f6e07961b1d2db93c/examples/anthropic_message_server.py) contains reviewed schemas, the placed Function, provider ordering, batch handling, limits, 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. Anthropic safety checks do not cover an application-owned batch tool.
</Warning>


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