> ## Documentation Index
> Fetch the complete documentation index at: https://docs.orq.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Apply patch server tool

> Let a model propose validated file changes while the application keeps control of filesystem writes.

The `orq:apply_patch` tool lets a model propose file changes. **Orq.ai** validates the operation, path, and diff. The application receives each valid patch as a pending function call and decides whether to apply it.

This is a human-in-the-loop tool. **Orq.ai** never writes to the application filesystem.

## Quick start

The examples use the client configuration from the [Server tools overview](/docs/ai-gateway/features/server-tools). The Responses API is used here because it exposes the pending call and follow-up output directly.

<CodeGroup>
  ```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl -X POST https://api.orq.ai/v3/router/responses \
    -H "Authorization: Bearer $ORQ_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "openai/gpt-5.4-mini",
      "input": "Create hello.txt containing the line Hello from Orq.ai",
      "tools": [
        { "type": "orq:apply_patch" }
      ]
    }'
  ```

  ```typescript TypeScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
  const response = await client.responses.create({
    model: 'openai/gpt-5.4-mini',
    input: 'Create hello.txt containing the line Hello from Orq.ai',
    tools: [{ type: 'orq:apply_patch' }] as any,
  });

  console.log(response.output);
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
  response = client.responses.create(
      model="openai/gpt-5.4-mini",
      input="Create hello.txt containing the line Hello from Orq.ai",
      tools=[{"type": "orq:apply_patch"}],
  )

  print(response.output)
  ```
</CodeGroup>

## Patch operations

The model supplies three arguments in the pending call:

| Argument    | Type   | Description                                            |
| ----------- | ------ | ------------------------------------------------------ |
| `operation` | string | `create_file`, `update_file`, or `delete_file`.        |
| `path`      | string | File path. Paths containing `..` are rejected.         |
| `diff`      | string | V4A-style diff. Use an empty string for `delete_file`. |

Validation depends on the operation:

* `create_file`: every non-empty diff line must start with `+`.
* `update_file`: lines may start with a space, `+`, `-`, or `@@`, and the diff must contain an addition or deletion.
* `delete_file`: the diff must be empty.

Malformed patches are returned to the model for correction inside the tool loop. Only a valid patch reaches the application.

## Apply the patch

Read the pending `apply_patch` function call, review and apply it in the application, then submit a standard function-call output. See [Tool calling and function execution](/docs/ai-gateway/features/tool-calling) for the response loop.

Apply patch has no additional charge beyond model tokens. Calls are not included in `usage.server_tool_use`.
