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

# Apply Code

> Merge code snippets from an LLM into your existing codebase.

Relace Apply runs at over **10,000 tok/s**, making complicated code merges feel instantaneous.

## Models

We have deprecated all previous versions of the apply models in favor of `relace-apply-3`, which is more performant on accuracy, speed, and long context requests.

| Model | Speed | Max Input | Max Output |
| :- | :- | :- | :- |
| `relace-apply-3` | \~10k tokens/s | 128k tokens | 128k tokens |

## OpenAI Compatible Endpoint

If the Relace REST API is inconvenient, we also support an OpenAI compatible endpoint for our apply models.

```typescript theme={null}
import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: 'YOUR_API_KEY',
  baseURL: 'https://models.relace.ai/v1/apply',
});

const userMessage = `
<instruction>{instruction}</instruction>
<code>{initial_code}</code>
<update>{edit_snippet}</update>
`;

const response = await client.chat.completions.create({
  model: 'relace-apply-3',
  messages: [
    {
      role: 'user',
      content: userMessage,
    },
  ],
});
```

<Warning>
  The user message must include `<code>` and `<update>` tags following the format above. The `<instruction>` tag is optional.
</Warning>

### OpenRouter

Relace Apply is also available via OpenRouter's OpenAI-compatible endpoint. See the [model page](https://openrouter.ai/relace/relace-apply-3) for current availability and details.

The [performance](https://openrouter.ai/relace/relace-apply-3/performance) section of the model page shows our real-time throughput and latency numbers from customer requests.

## Fallbacks

We recommend using GPT-4.1-mini with [predictive edits](https://platform.openai.com/docs/guides/predicted-outputs) as a fallback. This option is 10-20x slower than Relace's apply models, but it's useful for redundancy.

Relace apply models also return a `400` error code when input exceeds context limits (see table above). For these cases, GPT-4o-mini's 1M token context window is a reliable fallback option.

However, even frontier LLMs struggle with long context. We recommend proactive refactoring of files >32k tokens to improve output quality.


## OpenAPI

````yaml POST /v1/code/apply
openapi: 3.0.1
info:
  title: Relace API
  description: API for accessing Relace code generation models.
  version: 1.0.0
  license:
    name: MIT
servers:
  - url: https://models.relace.ai
    description: Server for model API endpoints
  - url: https://api.relace.run
    description: Server for general infrastructure
security:
  - bearerAuth: []
paths:
  /v1/code/apply:
    post:
      description: Merge code snippets from an LLM into your existing codebase.
      requestBody:
        description: Initial code and edits to apply
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/InstantApplyRequest'
            example:
              initial_code: |-
                function calculateTotal(items) {
                  let total = 0;
                  
                  for (const item of items) {
                    total += item.price * item.quantity;
                  }
                  
                  return total;
                }
              edit_snippet: |-
                // ... keep existing code

                function applyDiscount(total, discountRules) {
                  let discountedTotal = total;
                  
                  if (discountRules.percentOff) {
                    discountedTotal -= (total * discountRules.percentOff / 100);
                  }
                  
                  if (discountRules.fixedAmount && discountRules.fixedAmount < discountedTotal) {
                    discountedTotal -= discountRules.fixedAmount;
                  }
                  
                  return Math.max(0, discountedTotal);
                }
              stream: false
      responses:
        '200':
          description: Code successfully applied
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/InstantApplyResponse'
            text/event-stream:
              schema:
                type: string
                description: >-
                  Stream of results from the Instant Apply model (compatible
                  with OpenAI streaming format)
        '400':
          description: Bad request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error400'
        '401':
          description: Unauthorized
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error401'
        '402':
          description: Out of credits, or no payment method on the account
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error402'
        '404':
          description: Route not found
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error404'
        '429':
          description: Rate limit exceeded, or the model is at capacity
          headers:
            Retry-After:
              schema:
                type: string
              description: Seconds to wait before retrying
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error429'
        '500':
          description: Internal server error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error500'
        '502':
          description: Model server error
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error502'
        '503':
          description: Model temporarily unavailable
          headers:
            Retry-After:
              schema:
                type: string
              description: Seconds to wait before retrying
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error503'
        '504':
          description: Request to the model timed out
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Error504'
      servers:
        - url: https://models.relace.ai
components:
  schemas:
    InstantApplyRequest:
      type: object
      required:
        - initial_code
        - edit_snippet
      properties:
        model:
          type: string
          description: Choice of apply model to use
          enum:
            - relace-apply-3
          default: relace-apply-3
        initial_code:
          type: string
          description: The original code that needs to be modified
        edit_snippet:
          type: string
          description: The code changes to be applied to the initial code
        instruction:
          type: string
          description: >-
            Optional single line instruction for to disambiguate the edit
            snippet. *e.g.* `Remove the captcha from the login page`
        stream:
          type: boolean
          description: Whether to stream the response back
          default: false
        relace_metadata:
          type: object
          description: Optional metadata for logging and tracking purposes.
          additionalProperties: true
    InstantApplyResponse:
      type: object
      properties:
        mergedCode:
          type: string
          description: The merged code with the changes applied
        usage:
          type: object
          properties:
            prompt_tokens:
              type: integer
              description: Number of tokens in the prompt
            completion_tokens:
              type: integer
              description: Number of tokens in the completion
            total_tokens:
              type: integer
              description: Total number of tokens used
          description: Token usage information for the request
      example:
        mergedCode: |-
          function calculateTotal(items) {
            let total = 0;
            
            for (const item of items) {
              total += item.price * item.quantity;
            }
            
            return total;
          }

          function applyDiscount(total, discountRules) {
            let discountedTotal = total;
            
            if (discountRules.percentOff) {
              discountedTotal -= (total * discountRules.percentOff / 100);
            }
            
            if (discountRules.fixedAmount && discountRules.fixedAmount < discountedTotal) {
              discountedTotal -= discountRules.fixedAmount;
            }
            
            return Math.max(0, discountedTotal);
          }
        usage:
          prompt_tokens: 245
          completion_tokens: 187
          total_tokens: 432
    Error400:
      type: object
      properties:
        error:
          type: string
          description: Error message
          example: Invalid JSON in the request body.
    Error401:
      type: object
      properties:
        error:
          type: string
          description: Error message
          example: >-
            Invalid API key. Check the key, or create one at
            https://app.relace.ai.
    Error402:
      type: object
      properties:
        error:
          type: string
          description: Error message
          example: Out of credits. Add credits at https://app.relace.ai to continue.
    Error404:
      type: object
      properties:
        error:
          type: string
          description: Error message
          example: 'Route not found: POST /v1/code/edit'
    Error429:
      type: object
      properties:
        error:
          type: string
          description: Error message
          example: >-
            Rate limit exceeded. Retry after the Retry-After interval, or
            contact support to raise your limits.
    Error500:
      type: object
      properties:
        error:
          type: string
          description: Error message
          example: Internal server error.
    Error502:
      type: object
      properties:
        error:
          type: string
          description: Error message
          example: Model server error; retry shortly.
    Error503:
      type: object
      properties:
        error:
          type: string
          description: Error message
          example: Model 'relace-apply-3' is temporarily unavailable; retry shortly.
    Error504:
      type: object
      properties:
        error:
          type: string
          description: Error message
          example: Request to the model timed out after 60s.
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: Relace API key Authorization header using the Bearer scheme.

````

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