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

# Agents as code

> Declare agents and their Tilde configuration in TypeScript with @trytilde/iac, review the plan, and apply it with tilde deploy in one transaction.

With `@trytilde/iac`, you write an agent's Tilde configuration in TypeScript, next to its code. `tilde deploy` reads it, checks everything it references, and makes Tilde match it in one transaction. It works in every edition.

A spec covers the agent, its inference connections, tools, sandbox blueprint, routines, and the deployment you release. It can also declare people: see [People](https://trytilde.ai/docs/deployment/iac/people.md).

```bash theme={"system"}
pnpm add @trytilde/iac
```

## Declare an agent

Write the agent's configuration as a plain `Agent` object, so the same configuration can be applied on its own or with a project:

```ts src/spec.ts theme={"system"}
import { github, openai, routine, type Agent } from "@trytilde/iac";

export const triage = {
  key: "triage",
  name: "Triage",
  inference: { default: openai.ref("production") },
  tools: [github.ref("acme").tools("github_get_issue", "github_add_issue_comment")],
  routines: [
    routine({ name: "weekly-summary", prompt: "Summarise this week's new issues.", cron: "0 9 * * 1", enabled: true }),
  ],
} satisfies Agent;
```

The agent's entry module exports it with `defineAgent`, adding where its code runs, and hosts the agent:

```ts src/index.ts theme={"system"}
import { defineAgent, lambda } from "@trytilde/iac";
import { serve } from "@trytilde/sdk";
import { run } from "./agent.js";
import { triage } from "./spec.js";

export const spec = defineAgent({ ...triage, deploy: lambda(process.env.TRIAGE_FUNCTION_ARN!) });

export const handler = serve({ run });
```

* `key` is the agent's identity in code. Lowercase letters, digits, and dashes.
* `deploy` registers code you already deployed: `lambda(arn)`, `gateway()`, or `sidecar()`. Tilde does not host it. Prefer a version-qualified Lambda ARN, so the deployment stays fixed.
* `serve({ run })` hosts the agent wherever the module runs: inside AWS Lambda it is the handler, with a deployment token it dials in to the gateway, and on a plain import, such as while `tilde deploy` reads the module, it does nothing.
* `tilde deploy` reads the spec from the entry module, beside the prompts and skills it already reads.

## Plan and apply

```bash theme={"system"}
export TILDE_URL="https://<your-tilde-url>"
export TILDE_API_KEY="<key>"     # Tilde Cloud and Enterprise. Open-source Tilde needs none.

tilde deploy --dry-run           # the plan, nothing applied
tilde deploy                     # apply
```

```text theme={"system"}
Plan (dry run, nothing applied):
  + agent triage
  + tool_source triage/github/acme
      tools: github_get_issue, github_add_issue_comment
  + routine triage/weekly-summary
  1 unchanged
```

1. **Preflight.** Tilde checks every reference first: each connection exists, is ready, and offers the tools you name, and each secret exists. It lists every action you must take, each with a link. If there are any, the command exits with code `2`, and nothing is applied.
2. **One transaction.** Every change applies together, or none does. Repeating an apply with nothing to change reports everything as unchanged.

Add `--json` for output that scripts and agents can read.

## How updates work

Tilde finds each resource by its key, and updates it to match the spec, whoever created it, in the UI or in code. The plan shows each update first.

| Resource | Key |
| - | - |
| Agent | `key` |
| Inference alias | The alias, within its agent |
| Tool source | The connection, within its agent |
| Sandbox blueprint | `name` |
| Routine | `name`, within its agent |

A routine without `enabled` is created disabled, and later applies leave it to people. Changing a key creates a new resource, and leaves the old one.

## Remove something

Nothing is removed because it disappeared from your code. Keep the entry, and mark it with `delete: true`:

```ts theme={"system"}
tools: [
  github.ref("acme").tools("github_get_issue"),
  connection.ref("linear/acme").tools({ delete: true }),   // the agent stops using Linear's tools
],
```

Routines, inference aliases, blueprint variables, egress grants, users, and groups take `delete: true` the same way. Removing something already gone does nothing, so keep the entry until every environment has applied it. To delete an agent itself, use the [Agent Registry](https://trytilde.ai/docs/agent-registry.md).

## Several agents: `defineProject`

`defineProject` applies several agents, the team they belong to, and the project's [ontologies](https://trytilde.ai/docs/ontology/deploying.md), in one `tilde deploy`. A [Tilde project](https://trytilde.ai/docs/project/overview.md) exports it from `tilde.config.ts`, with the same `Agent` objects each agent's `spec.ts` exports.

```ts tilde.config.ts theme={"system"}
import { defineAgent, defineProject } from "@trytilde/iac";
import { support } from "./agents/support/src/spec.ts";
import { triage } from "./agents/triage/src/spec.ts";

export const project = defineProject({
  team: "support",                                  // Tilde Cloud and Enterprise
  agents: [defineAgent(support), defineAgent(triage)],
});
```

One entry module registers one agent's code, so each agent's release is registered from its own folder, where its `index.ts` adds `deploy`. See [Release the agent](https://trytilde.ai/docs/project/deploy.md#release-the-agent).

## Next steps

> [card] [Connections, secrets, and tools](https://trytilde.ai/docs/deployment/iac/connections-and-tools.md)
>
> Reference what Tilde holds, and pick tools with typed names.

> [card] [Deploy from CI](https://trytilde.ai/docs/project/deploy.md#deploy-from-ci)
>
> Check on pull requests, and deploy on merge.


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