> ## 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 Agents with Terraform

> Provision and update Runlayer Agents from Terraform with the runlayer_agent resource: prompt, model, connectors, skills, built-in tools, tags, and environment.

Agents can be managed as code with the [Runlayer Terraform provider](https://docs.runlayer.com/terraform-provider.md). The `runlayer_agent` resource owns the agent's configuration; the `runlayer_agent` data source reads back what Runlayer computed (model, generated description, counts).

## What you need

* The provider set up as described in [Terraform Provider](https://docs.runlayer.com/terraform-provider.md), with a **user** API key. Organization API keys cannot create agents.
* A provider release that includes `runlayer_agent` (check the provider changelog on `terraform init`).
* Agents enabled for your workspace.

## Example

A support-triage agent with a least-privilege connector, a prompt kept in a file, tags, an environment variable, and starter prompts. It starts disabled so nothing can trigger it before you have reviewed it in the app.

```hcl theme={null}
terraform {
  required_providers {
    runlayer = {
      source  = "downloads.runlayer.com/runlayer/runlayer"
      version = "~> 1.8, >= 1.8.2"
    }
  }
}

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

# The connector the agent may use. Reference an existing server, or manage
# it with a runlayer_server resource in the same configuration.
data "runlayer_server" "linear" {
  server_id = var.linear_server_id
}

resource "runlayer_agent" "support_triage" {
  name = "support-triage"

  # The API strips leading/trailing whitespace from the prompt, so trimspace()
  # keeps file()'s trailing newline from showing up as a permanent diff.
  prompt = trimspace(file("${path.module}/prompts/support-triage.md"))

  # Returned sorted: list them sorted to avoid a permanent diff.
  tags = ["managed-by-terraform", "support"]

  # model = "claude-sonnet-5"   # omit to use the workspace's recommended model

  example_prompts = [
    "Triage the newest ticket in the support queue.",
    "Is ticket SUP-1234 a duplicate?",
  ]

  # Least-privilege tool allowlist per connector.
  # An empty tool_names means zero tools, not all tools.
  servers = [{
    server_id  = data.runlayer_server.linear.id
    tool_names = ["search_issues", "get_issue"]
  }]

  # Reusable instructions attached to the agent.
  skill_ids = [var.triage_skill_id]

  # Available to the agent runtime. Sensitive in plan/apply output; the API
  # never returns values, only env_keys.
  env = {
    TRIAGE_QUEUE = "support"
  }

  # Start parked; flip to false to let triggers run it.
  is_disabled = true
}

data "runlayer_agent" "support_triage" {
  agent_id = runlayer_agent.support_triage.id
}

output "support_triage" {
  value = {
    id           = runlayer_agent.support_triage.id
    model        = data.runlayer_agent.support_triage.model
    description  = data.runlayer_agent.support_triage.description
    server_count = data.runlayer_agent.support_triage.server_count
    tool_count   = data.runlayer_agent.support_triage.tool_count
    env_keys     = data.runlayer_agent.support_triage.env_keys
  }
}
```

`prompts/support-triage.md`:

```markdown theme={null}
You are the support triage agent for Runlayer.

For every incoming ticket:
1. Search Linear for duplicates before doing anything else.
2. Classify severity (P0-P3) using the playbook, and say why in one line.
3. Draft a reply for the support engineer to send. Never send it yourself.

Keep answers short. If you are unsure about severity, pick the higher one.
```

```bash theme={null}
terraform init
terraform apply
```

After `apply`, the agent appears in the app under **Agents**, owned by the user whose API key Terraform used, or by `owner_id` when you set it. Open it there to run it, share it, add triggers, and watch sessions. Those runtime settings are not managed by Terraform.

## What Terraform manages

| Attribute | Notes |
| - | - |
| `name`, `prompt` | Prompt whitespace is trimmed by the API; use `trimspace()`. |
| `model`, `ai_provider_config_id`, `model_settings` | Optional. Omit `model` for the workspace's [recommended model](https://docs.runlayer.com/platform-agents.md#recommended-model). |
| `servers` | List of `{ server_id, tool_names }`. Full replacement on every apply. |
| `skill_ids` | Skill UUIDs to attach. Full replacement on every apply. |
| `enable_artifact_publishing`, `artifact_sharing`, `enable_codemode`, `enable_skill_distillation`, `enable_memory`, `memory_scopes` | Built-in tools and [runtime capabilities](https://docs.runlayer.com/platform-agents.md#runtime-capabilities). |
| `env` | Environment variables for the runtime. Sensitive. |
| `tags`, `example_prompts`, `subagents`, `pending_catalog_connectors`, `icon` | Returned as-is; keep `tags` sorted. |
| `is_public`, `listed_in_workspace`, `is_disabled` | Visibility and on/off switch. |
| `enable_slack_budget_alerts`, `enable_slack_scheduled_run_failure_alerts` | [Budget alerts](https://docs.runlayer.com/platform-agents.md#budget-alerts). |
| `owner_id` | User id of the agent's owner. Unset at create, the API key's user owns it; removing `owner_id` later leaves the owner as it is. `null` means a team or the org owns the agent. A different user transfers ownership, which needs permission to transfer ownership (admins have it), and an edit in the app shows up as drift. The API key's user must keep access afterwards (an agent manager does): a hand-over that would lock Terraform out is refused and the request saves nothing. If the agent uses connectors, the new owner may need to reconnect them in the app. |
| `open_to_collaboration` | Lets the agent work with agents that act for other people in the workspace, for example to find a meeting time. Each side acts only for its own person. Provider 1.8.1 or later, backend 1.28.1590 or later. Leave it out of config to keep the value set in the app. |

Read-only in Terraform, managed in the app: `description` (generated by Runlayer from the prompt), `harness`, and the [usage limits](https://docs.runlayer.com/platform-agents.md#cost-budgets) `run_limits_by_model`, `daily_cost_limit_usd`, `weekly_cost_limit_usd`. The data source also exposes `server_count`, `tool_count`, `skill_count`, `env_keys`, `skills`, `created_by`, `share_scope`, and timestamps.

Provider 1.8.1 and later also return two read-only usage fields for the user whose API key Terraform uses: `my_last_used_at`, your latest run of the agent (null if you never ran it), and `my_run_count`, the runs you started in the last 30 days. They need backend 1.28.1543 or later.

> [!NOTE]
>
> `servers`, `skill_ids`, and `env` are request-only: Terraform sends them on create and update but does not read them back, because the API returns connectors in a different shape and never returns environment values. Edits made to those three in the app are not detected as drift. Use `server_count`, `tool_count`, `skill_count`, and `env_keys` from the data source for read-side checks.

## Catching changes made in the app

An agent you reviewed and applied from Terraform can still be edited in the app by anyone with edit access. Two checks catch those edits, including the connector, tool and skill changes `terraform plan` can't see.

**Pin the reviewed version.** Every saved change to what the agent is and does creates a new [version](https://docs.runlayer.com/platform-agents.md#version-history): prompt, model, network access policy, memory scopes, code mode, delegation, connectors with their tool lists, skills and environment variable names. Visibility switches (`is_public`, `listed_in_workspace`, `is_disabled`) and usage limits don't. The data source returns the latest version as `current_version`. Record the version you reviewed and flag the plan when it moves (`check` blocks need Terraform 1.5 or later):

```hcl theme={null}
variable "support_triage_reviewed_version" {
  type = number
}

check "support_triage_unchanged" {
  assert {
    condition     = data.runlayer_agent.support_triage.current_version == var.support_triage_reviewed_version
    error_message = "support-triage changed since review: now v${data.runlayer_agent.support_triage.current_version}, reviewed v${var.support_triage_reviewed_version}. Check its version history before you approve the plan."
  }
}
```

An apply that changes the agent creates a version too, so update the variable after each reviewed apply. A `check` reports a warning and doesn't stop the apply; to block, make the same assertion a `precondition` on a resource that depends on the agent.

**Alert on edits as they happen.** Each edit writes an `agent_updated` [audit log](https://docs.runlayer.com/platform-audit-logs.md) entry with the changed fields, their old values and the version it created. Stream these to your SIEM, or add an [Audit event trigger](https://docs.runlayer.com/platform-agents.md#set-up-an-agent) for `agent_updated` to a reviewer agent. These entries also cover the visibility switches that don't create versions. Fields worth watching: `model`, `egress_policy`, `memory_scopes`, `enable_codemode`, `can_delegate`, `connectors` and `is_public`.

These checks detect a change after it's saved. They don't block it. Runlayer doesn't yet enforce an organization-wide baseline for agent configuration. What admins can restrict today: which roles may use each model (**Settings → AI Providers**, see [LLM providers](https://docs.runlayer.com/platform-agents.md#llm-providers)), which [memory scopes](https://docs.runlayer.com/platform-agents.md#memory-scopes) agents may use (**Settings → Memory**), and what an agent's connectors may do at run time, through [policies](https://docs.runlayer.com/platform-policies.md). None of these are Terraform resources yet.

## Importing an existing agent

```bash theme={null}
terraform import runlayer_agent.support_triage <agent-id>
```

The agent id is the UUID in the app URL (`/agents/<agent-id>`). The first plan after an import shows a one-time in-place update that re-sends `servers`, `skill_ids`, and `env` from your configuration, for the reason above.

## Deleting

`terraform destroy` deletes the agent and its linked agent account. Sessions, artifacts, and audit history follow the same retention as a deletion from the app.

## Related docs

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

> [card] [Agents](https://docs.runlayer.com/platform-agents.md)
>
> Everything an agent can do once it exists

> [card] [Policies](https://docs.runlayer.com/platform-policies.md)
>
> Control what agents and users can do

> [card] [Agent Accounts](https://docs.runlayer.com/platform-agent-accounts.md)
>
> Programmatic auth + delegated access model


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