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

# Manage AI Providers and the LLM Gateway with Terraform

> Manage AI providers (including Vertex AI), the recommended model, and LLM gateway usage rules, routing rules and virtual keys with the Runlayer Terraform provider.

The [Runlayer Terraform provider](https://docs.runlayer.com/terraform-provider.md) manages what admins set up in **Settings → AI Providers** and **Settings → LLM Gateway**:

| Resource | What it manages |
| - | - |
| `runlayer_ai_provider` | One AI provider: Anthropic, OpenAI, an OpenAI-compatible gateway, AWS Bedrock, Azure Foundry, Google AI Studio or Google Vertex AI, with its models and which roles may use each model |
| `runlayer_ai_provider_recommended_model` | The workspace's [recommended model](https://docs.runlayer.com/platform-agents.md#recommended-model) |
| `runlayer_llm_gateway_usage_rule` | Whether a scope routes through the gateway, and its budgets and alert thresholds |
| `runlayer_llm_gateway_routing_rule` | Which provider and model serve a requested model, and the fallbacks to try |
| `runlayer_llm_gateway_virtual_key` | A gateway key with its own budget, for example for CI |

Each resource has a data source of the same name that reads one existing object.

## What you need

* Runlayer provider 1.17.0 or later. Earlier releases don't have these resources.
* An API key whose user may manage AI providers and LLM gateway settings, set up as in [Terraform Provider](https://docs.runlayer.com/terraform-provider.md). Admins can.
* For usage rules, routing rules and virtual keys: the LLM gateway on for your workspace.

```hcl theme={null}
terraform {
  required_providers {
    runlayer = {
      source  = "downloads.runlayer.com/runlayer/runlayer"
      version = ">= 1.17.0, < 2.0.0"
    }
  }
}

# Reads RUNLAYER_API_KEY and RUNLAYER_BASE_URL from the environment.
provider "runlayer" {}
```

## AI providers

```hcl theme={null}
variable "anthropic_api_key" {
  type      = string
  sensitive = true
}

variable "litellm_api_key" {
  type      = string
  sensitive = true
}

variable "vertex_service_account_key" {
  type      = string
  sensitive = true
}

resource "runlayer_ai_provider" "anthropic" {
  name    = "Anthropic"
  type    = "anthropic"
  api_key = var.anthropic_api_key
}

# A self-hosted OpenAI-compatible gateway serving two models. Only
# developers may select "finance".
data "runlayer_role" "developer" {
  role_type = "developer"
}

resource "runlayer_ai_provider" "litellm" {
  name     = "LiteLLM"
  type     = "openai_compatible"
  api_key  = var.litellm_api_key
  base_url = "https://llm.example.com/v1"
  models   = ["support", "finance"]
  model_role_restrictions = {
    finance = [data.runlayer_role.developer.id]
  }
}

# Vertex AI, for the LLM gateway only. The key is the contents of the
# service account's JSON key file.
resource "runlayer_ai_provider" "vertex" {
  name                       = "Vertex"
  type                       = "vertex"
  vertex_project_id          = "my-gcp-project"
  vertex_region              = "us-east5"
  vertex_service_account_key = var.vertex_service_account_key
}

resource "runlayer_ai_provider_recommended_model" "this" {
  model       = "support"
  provider_id = runlayer_ai_provider.litellm.id
}
```

* **Credentials**: `api_key` and `vertex_service_account_key` are sensitive, and the API never returns them. Terraform sends what your configuration holds, so a key changed in the app isn't detected as drift. After an import, the first apply sends the configured key once.
* **Changing `base_url`**: the apply also sends `api_key` from your configuration, because Runlayer refuses to move a stored key to a new URL. Keep the key in the configuration, even with `lifecycle { ignore_changes = [api_key] }`.
* **Model access**: `model_role_restrictions` maps a model to the role IDs that may select it. A model left out of the map is open to everyone. Terraform manages the whole map, so restrictions set in the app on other models are removed on the next apply.
* **Vertex AI**: takes a service account key, a project and a region, and no `api_key` or `base_url`. Agents and the Runlayer Assistant don't use it; see [LLM providers](https://docs.runlayer.com/platform-agents.md#llm-providers) for the models it serves and how to rotate its key.
* **Recommended model**: an organization-wide setting, so declare it once. `model` must be one of `provider_id`'s models. It has no import: apply it with the values you want. Destroy clears the recommendation.

## LLM gateway

```hcl theme={null}
# Organization root: a $50 monthly cap per device key and a $10,000 monthly
# org-wide budget, with admins emailed at 50%, 80% and 100% of it.
resource "runlayer_llm_gateway_usage_rule" "organization" {
  scope                = "organization"
  key_budget_usd       = 50
  aggregate_budget_usd = 10000
  budget_window        = "monthly"
  alert_thresholds_pct = [50, 80, 100]
}

resource "runlayer_team" "platform" {
  name               = "Platform"
  default_permission = "can_use"
}

resource "runlayer_llm_gateway_usage_rule" "platform_team" {
  scope                = "team"
  subject_id           = runlayer_team.platform.id
  priority             = 10
  aggregate_budget_usd = 2000
  budget_window        = "monthly"
  alert_thresholds_pct = [90]
}

# Org default list: serve Opus requests with Sonnet, and fail over to Haiku
# while Sonnet is down or rate limited.
resource "runlayer_llm_gateway_routing_rule" "opus_to_sonnet" {
  model_pattern   = "claude-opus-*"
  target_provider = "anthropic"
  target_model    = "claude-sonnet-5-5"
  position        = 0

  fallbacks = [
    { provider_name = "anthropic", model = "claude-haiku-5-5" },
  ]

  depends_on = [runlayer_ai_provider.anthropic]
}

# Once the platform team's budget is spent, downgrade to Haiku instead of
# blocking.
resource "runlayer_llm_gateway_routing_rule" "platform_over_budget" {
  model_pattern   = "*"
  target_provider = "anthropic"
  target_model    = "claude-haiku-5-5"
  condition       = "over_budget"
  usage_rule_id   = runlayer_llm_gateway_usage_rule.platform_team.id

  depends_on = [runlayer_ai_provider.anthropic]
}

# A CI key hard-capped at $200.
resource "runlayer_llm_gateway_virtual_key" "ci" {
  name          = "ci-pipeline"
  budget_usd    = 200
  budget_action = "block"
}

output "ci_gateway_key" {
  value     = runlayer_llm_gateway_virtual_key.ci.key
  sensitive = true
}
```

* **Usage rules**: `scope` is `organization`, `team`, `group`, `user`, `agents` or `virtual_key`, and `subject_id` names the team, group, user or virtual key. `priority` breaks ties between team and group rules that cover the same user. `routing = "no_route"` takes a scope out of the gateway.
* **Routing rules**: the gateway tries rules by ascending `position`, and the first match wins. Setting a position never renumbers other rules. Every target and fallback must be a model on the AI providers card, so make a rule depend on the `runlayer_ai_provider` that serves its models. A rule with `usage_rule_id` belongs to that usage rule's override list, and `condition = "over_budget"` applies it only once that budget is spent.
* **Virtual keys**: the plaintext `key` is returned only when the key is created, so read it from the output then; it is null after an import. Set `budget_action` whenever you set `budget_usd`.

## Import

Every resource except `runlayer_ai_provider_recommended_model` imports by its ID:

```bash theme={null}
terraform import runlayer_ai_provider.litellm '<provider_id>'
terraform import runlayer_llm_gateway_usage_rule.platform_team '<rule_id>'
terraform import runlayer_llm_gateway_routing_rule.opus_to_sonnet '<rule_id>'
terraform import runlayer_llm_gateway_virtual_key.ci '<key_id>'
```

To bring many objects under Terraform and let it write their configuration, see [Adopt Existing Resources](https://docs.runlayer.com/terraform-provider-adoption.md).

## Related docs

> [card] [Terraform Provider](https://docs.runlayer.com/terraform-provider.md)
>
> Provider setup, credentials, and the other resources

> [card] [LLM providers](https://docs.runlayer.com/platform-agents.md#llm-providers)
>
> Which providers and models Runlayer supports


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