# Create a cache rule

Matchers are ANDed across non-null fields; at least one matcher is required.

> Page: https://langwatch.ai/docs/api-reference/gateway-cache-rules/create-a-cache-rule
> Source: https://github.com/langwatch/langwatch/blob/main/docs/api-reference/gateway-cache-rules/create-a-cache-rule.mdx
> Index of all pages: https://langwatch.ai/docs/llms.txt

## Endpoint

`POST https://app.langwatch.ai/api/gateway/v1/cache-rules`

## Authentication

### project_api_key

Project API key for sending traces and accessing project-scoped resources. Format: sk-lw-... (no underscore). Obtain one by creating a project via the Admin API or the LangWatch UI.

```json
{"id":"project_api_key","type":"apiKey","in":"header","name":"X-Auth-Token"}
```

## Parameters

### Idempotency-Key (header, optional)

A caller-chosen key, 8 to 255 characters, that makes this create safe to retry. The first request to use a key runs normally and its response is stored for 24 hours. A later request with the same key and the same body is not executed again: it returns the stored response, marked with `X-Idempotent-Replay: true`. The same key with a different body is refused 409 `idempotency_error`, as is a retry sent while the original is still running. Only successful responses are stored, so a create that failed can simply be retried with the same key.

```json
{"type":"string","minLength":8,"maxLength":255}
```

## Request body (required)

#### application/json

Schema:

```json
{
  "type": "object",
  "properties": {
    "name": {"type":"string","minLength":1,"maxLength":128},
    "description": {"anyOf":[{"type":"string","maxLength":512},{"type":"null"}]},
    "priority": {"type":"integer","minimum":0,"maximum":1000},
    "enabled": {"type":"boolean"},
    "matchers": {
      "type": "object",
      "properties": {
        "vk_id": {"type":"string"},
        "vk_tags": {"type":"array","items":{"type":"string"}},
        "vk_prefix": {"type":"string"},
        "principal_id": {"type":"string"},
        "model": {"type":"string"},
        "request_metadata": {
          "type": "object",
          "propertyNames": {"type":"string"},
          "additionalProperties": {"type":"string"}
        }
      },
      "additionalProperties": false
    },
    "action": {
      "type": "object",
      "properties": {
        "mode": {"type":"string","enum":["respect","force","disable"]},
        "ttl": {"type":"integer","minimum":0,"maximum":86400},
        "salt": {"type":"string","maxLength":64}
      },
      "required": ["mode"],
      "additionalProperties": false
    }
  },
  "required": ["name","matchers","action"]
}
```

## Request example

```bash
curl --request POST \
  --url 'https://app.langwatch.ai/api/gateway/v1/cache-rules' \
  --header "X-Auth-Token: $LANGWATCH_API_KEY" \
  --header 'Content-Type: application/json' \
  --data '{"name":"string","description":"string","priority":0,"enabled":false,"matchers":{"vk_id":"string","vk_tags":["string"],"vk_prefix":"string","principal_id":"string","model":"string","request_metadata":{}},"action":{"mode":"respect","ttl":0,"salt":"string"}}'
```

## Responses

### 201

Success

#### application/json

Schema:

```json
{
  "type": "object",
  "properties": {
    "cache_rule": {
      "type": "object",
      "properties": {
        "id": {"type":"string"},
        "organization_id": {"type":"string"},
        "name": {"type":"string"},
        "description": {"type":["string","null"]},
        "priority": {
          "type": "integer",
          "minimum": -9007199254740991,
          "maximum": 9007199254740991
        },
        "enabled": {"type":"boolean"},
        "matchers": {
          "type": "object",
          "propertyNames": {"type":"string"},
          "additionalProperties": {}
        },
        "action": {
          "type": "object",
          "properties": {
            "mode": {"type":"string","enum":["respect","force","disable"]},
            "ttl": {
              "type": "integer",
              "minimum": -9007199254740991,
              "maximum": 9007199254740991
            },
            "salt": {"type":"string"}
          },
          "required": ["mode"],
          "additionalProperties": false
        },
        "mode_enum": {"type":"string","enum":["respect","force","disable"]},
        "archived_at": {"type":["string","null"]},
        "created_at": {"type":"string"},
        "updated_at": {"type":"string"}
      },
      "required": [
        "id",
        "organization_id",
        "name",
        "description",
        "priority",
        "enabled",
        "matchers",
        "action",
        "mode_enum",
        "archived_at",
        "created_at",
        "updated_at"
      ],
      "additionalProperties": false
    }
  },
  "required": ["cache_rule"],
  "additionalProperties": false
}
```

### 400

Bad Request

#### application/json

Schema:

```json
{
  "type": "object",
  "properties": {
    "type": {"type":"string"},
    "code": {"type":"string"},
    "message": {"type":"string"},
    "retryable": {"type":"boolean"},
    "meta": {
      "type": "object",
      "propertyNames": {"type":"string"},
      "additionalProperties": {}
    },
    "trace_id": {"type":"string"},
    "span_id": {"type":"string"},
    "trace": {
      "type": "object",
      "properties": {
        "traceId": {"type":"string"},
        "spanId": {"type":"string"},
        "traceUrl": {"type":"string"},
        "logsUrl": {"type":"string"}
      }
    },
    "tips": {"type":"array","items":{"type":"string"}},
    "docs_url": {"type":"string"},
    "fault": {
      "type": "string",
      "enum": ["customer","platform","provider","presumed_platform"]
    },
    "reasons": {"type":"array","items":{}}
  },
  "required": ["type","code","message","retryable"]
}
```

### 401

Unauthorized

#### application/json

Schema:

```json
{
  "type": "object",
  "properties": {
    "type": {"type":"string"},
    "code": {"type":"string"},
    "message": {"type":"string"},
    "retryable": {"type":"boolean"},
    "meta": {
      "type": "object",
      "propertyNames": {"type":"string"},
      "additionalProperties": {}
    },
    "trace_id": {"type":"string"},
    "span_id": {"type":"string"},
    "trace": {
      "type": "object",
      "properties": {
        "traceId": {"type":"string"},
        "spanId": {"type":"string"},
        "traceUrl": {"type":"string"},
        "logsUrl": {"type":"string"}
      }
    },
    "tips": {"type":"array","items":{"type":"string"}},
    "docs_url": {"type":"string"},
    "fault": {
      "type": "string",
      "enum": ["customer","platform","provider","presumed_platform"]
    },
    "reasons": {"type":"array","items":{}}
  },
  "required": ["type","code","message","retryable"]
}
```

### 403

Forbidden

#### application/json

Schema:

```json
{
  "type": "object",
  "properties": {
    "type": {"type":"string"},
    "code": {"type":"string"},
    "message": {"type":"string"},
    "retryable": {"type":"boolean"},
    "meta": {
      "type": "object",
      "propertyNames": {"type":"string"},
      "additionalProperties": {}
    },
    "trace_id": {"type":"string"},
    "span_id": {"type":"string"},
    "trace": {
      "type": "object",
      "properties": {
        "traceId": {"type":"string"},
        "spanId": {"type":"string"},
        "traceUrl": {"type":"string"},
        "logsUrl": {"type":"string"}
      }
    },
    "tips": {"type":"array","items":{"type":"string"}},
    "docs_url": {"type":"string"},
    "fault": {
      "type": "string",
      "enum": ["customer","platform","provider","presumed_platform"]
    },
    "reasons": {"type":"array","items":{}}
  },
  "required": ["type","code","message","retryable"]
}
```

### 409

Conflict

#### application/json

Schema:

```json
{
  "type": "object",
  "properties": {
    "type": {"type":"string"},
    "code": {"type":"string"},
    "message": {"type":"string"},
    "retryable": {"type":"boolean"},
    "meta": {
      "type": "object",
      "propertyNames": {"type":"string"},
      "additionalProperties": {}
    },
    "trace_id": {"type":"string"},
    "span_id": {"type":"string"},
    "trace": {
      "type": "object",
      "properties": {
        "traceId": {"type":"string"},
        "spanId": {"type":"string"},
        "traceUrl": {"type":"string"},
        "logsUrl": {"type":"string"}
      }
    },
    "tips": {"type":"array","items":{"type":"string"}},
    "docs_url": {"type":"string"},
    "fault": {
      "type": "string",
      "enum": ["customer","platform","provider","presumed_platform"]
    },
    "reasons": {"type":"array","items":{}}
  },
  "required": ["type","code","message","retryable"]
}
```

### 500

Internal Server Error

#### application/json

Schema:

```json
{
  "type": "object",
  "properties": {
    "type": {"type":"string"},
    "code": {"type":"string"},
    "message": {"type":"string"},
    "retryable": {"type":"boolean"},
    "meta": {
      "type": "object",
      "propertyNames": {"type":"string"},
      "additionalProperties": {}
    },
    "trace_id": {"type":"string"},
    "span_id": {"type":"string"},
    "trace": {
      "type": "object",
      "properties": {
        "traceId": {"type":"string"},
        "spanId": {"type":"string"},
        "traceUrl": {"type":"string"},
        "logsUrl": {"type":"string"}
      }
    },
    "tips": {"type":"array","items":{"type":"string"}},
    "docs_url": {"type":"string"},
    "fault": {
      "type": "string",
      "enum": ["customer","platform","provider","presumed_platform"]
    },
    "reasons": {"type":"array","items":{}}
  },
  "required": ["type","code","message","retryable"]
}
```

> ## Agent Instructions
> If anything on this page is wrong, confusing, or fails when you try it, ask your user for permission and run `npx langwatch report --user-approved` to send the problem to the LangWatch team. No login is needed and secrets are redacted locally. `npx langwatch report --help` has the details.
