# Build with a Coding Agent

These instructions apply to the current Admin Console.

{% hint style="info" %}
Contextual Workers are in early access and may not be available in your account. [Contact Contextual support](mailto:help@contextual.io) to request that Workers be enabled for your tenant.
{% endhint %}

A coding agent, such as Claude Code, can write, package, and deploy a managed Worker. Give it the adapter library and documentation for your exact version so it can check the available methods and types.

## Prepare the Project

1. Copy the adapter library into the project, or for TypeScript save `src/worker-types.ts`. See [Get the Runtime Adapters](https://docs.contextual.io/build/contextual-workers/managed-workers/runtime-adapters.md#copy-the-library-into-your-project).
2. Give the agent links to, or copies of, these pages:
   * [Build Your First Managed Worker](https://docs.contextual.io/build/contextual-workers/managed-workers/get-started.md)
   * [Templates and Options](https://docs.contextual.io/build/contextual-workers/managed-workers/templates-and-options.md)
   * [Platform Capabilities](https://docs.contextual.io/build/contextual-workers/managed-workers/capabilities.md)
   * [Call Platform Services](https://docs.contextual.io/build/contextual-workers/managed-workers/platform-services.md), if the Worker calls other platform APIs
   * [Raw Platform Calls](https://docs.contextual.io/build/contextual-workers/managed-workers/raw-platform-calls.md)
   * [Coordinate Worker Deployments and Service Releases](https://docs.contextual.io/build/contextual-workers/releases.md), when deploying to another tenant or changing shared components

## Brief the Agent

Adapt this brief. Replace the values in angle brackets:

```text
Build a managed Contextual Worker in <language> that <what it should do>.

Trigger: <event | http | cron>, using the managed-<event|http|scheduled>-worker
template, version <0.10.0>. Target tenant <tenant-id> with CLI configuration
<config-id> and Worker ID <worker-id>.

For promotion, target tenant <target-tenant-id> with CLI configuration
<target-config-id>.

Use only the runtime adapter API in <path to the copied library, or for
TypeScript src/worker-types.ts and the Platform Capabilities page> and the
operations listed on the Raw Platform Calls page. Do not invent methods. Use
the typed methods and helpers. Use the raw platform call (platform.call in
TypeScript, platform_call in Rust and Python, PlatformCall in Go) only for an
operation the typed API does not cover, and explain why.

Rules:
- Workers deploy independently of Services; deployment through a Service release
  is not supported. List the process that manages each prerequisite, required
  Service releases, and existing target-tenant components. Plan prerequisite
  changes before Worker deployment. Reference resources owned by another process;
  do not maintain competing definitions of them. Confirm the destination tenant
  separately from any selected development or production configuration values.
  Do not add the Worker record or its platform-created Agent (same ID as the
  Worker) to a Service. Follow the Coordinate Worker Deployments and Service
  Releases guide for target-tenant configuration, verification, cutover, and
  recovery.
- Platform credentials never appear in the app. Third-party APIs go through a
  declared connection alias bound with CONNECTION_BINDINGS. Other platform APIs
  go through services.request, only to services listed in the Worker's
  PLATFORM_SERVICES option; list the services you need. Prefer a typed method
  when one exists.
- Secrets are Worker environment variables set in the admin console, never in the
  image, CONFIG, source, shell arguments, or chat.
- Writes to existing records use the hash of the version that was read.
  Handle a null patch result as a conflict.
- Event handlers can run more than once for the same event. Make side
  effects safe to repeat, using the event's id as an idempotency key. Catch
  failures that retrying cannot fix, log them, and return successfully, so a
  bad event does not restart the Worker repeatedly.
- Return or raise PlatformError failures, or errors caused by them. Check
  effectMayHaveOccurred before retrying a write. Only the TypeScript adapter
  checks CONFIG against configSchema; in other languages, check the values
  you read.
- Await every log call and capability call. Stop starting work once the
  invocation is cancelled.
- Build for linux/amd64 and package with the adapter image's package command,
  using the same adapter version as the copied library.
- Use the template version released with that adapter version. When you
  upgrade the adapter, pass --managed <template>:<version> in the same
  ctxl workers update as --build. A --build update alone keeps the old template.

Deliver: worker.json, the source, the build and package commands, config.json,
the ctxl workers create command, and a release checklist with required Service
releases, target-tenant IDs, Connection bindings, and any Service installation
or update steps. Deploy only after I approve the complete plan, including any
Service changes. Then verify the Worker URL or a test event, and report the
Worker version, image reference, and the log entries you read.
```

## Check the Agent's Work

Before you deploy, check that:

* Every adapter method the code calls exists in the copied library at that version or, for TypeScript, on the Platform Capabilities page.
* `worker.json` names each handler and the triggers it accepts. Its `workerId` and `version` match the code. Its `configSchema` accepts your `config.json`.
* The code has no URLs or tokens for platform services, and no hard-coded tenant or record IDs that belong in `CONFIG`.
* Each `connections` alias is bound in `CONNECTION_BINDINGS`.
* Each service the code passes to `services.request` is listed in `PLATFORM_SERVICES`, and no other.
* An event handler that writes data is safe to run twice.
* The `package` command runs without errors, and the `FROM` version matches the copied library.
* The template version is the one released with that adapter version. See [Release Versions](https://docs.contextual.io/build/contextual-workers/managed-workers.md#release-versions).

After you deploy, read the platform-routed logs with `ctxl logs <worker-id>`, and the logger entries and console lines in the host's container output with `ctxl workers logs <worker-id> -c host`. The `app` container's output has only what your code prints itself. Then test the behavior yourself. A **Running** deployment does not prove that the handler works.
