Skip to content
AI Skills Central

Type to search Agent Skills, workflows, tutorials, tools and the glossary.

Agent Skill

Workers best practices (Cloudflare)

Checks Cloudflare Workers code against current platform rules: compatibility dates, observability, streaming, waitUntil, secrets and generated types.

name: workers-best-practicesBy CloudflareChecked
View source on GitHub
Bookmark

Bookmarks are kept in your free account, so you can find this page again on any device.

Sign in to bookmark

Install

Commands are pinned to commit 5108325, the version we read, wherever the install method supports it.

npx skills

Terminal
npx skills add cloudflare/skills@5108325ecceabc899c761e07aa617b09c02445ee --skill workers-best-practices -a claude-code

Copies the skill into .claude/skills/ in this project, pinned to the reviewed commit.

Plugin marketplace

Claude Code prompt
/plugin install cloudflare@claude-plugins-official

Run inside Claude Code. Installs the whole cloudflare plugin from its default branch, not the pinned commit; the official marketplace is available by default.

What it does

Cloudflare’s review checklist for Workers code. The first line sets the tone: the model’s knowledge of Workers APIs may be out of date, so prefer the project’s installed versions, generated types and Cloudflare’s documentation over memory. Three rules apply to every production Worker: keep the compatibility date current, turn on Workers Logs and traces, and set placement near a regional database when a Worker makes several sequential queries to it. Then comes a table of anti-patterns with the consequence of each, which is the most useful part for review. It covers buffering unbounded bodies with await response.text(), background work not tied to ctx.waitUntil(), module-level request state that leaks between requests, hand-written Env types that drift from wrangler types, Math.random() for tokens, plain string comparison of secrets, and destructuring ctx methods so they lose their receiver.

When to use it

Writing a new Worker, reviewing a pull request that touches one, or preparing an existing Worker for production. Each anti-pattern links to a section in the reference files with the preferred pattern, so the agent can explain its findings.

When to skip it

Durable Objects specifics and Wrangler commands: the skill hands those to the durable-objects and wrangler skills. It also says a narrow edit does not need a full Workers audit, which is a sensible limit.

Try it

Review src/worker.ts against the Workers best practices skill. List every
anti-pattern you find with the line number and the fix, and check whether
the compatibility date and observability settings in wrangler.jsonc are
production-ready.

What it can touch

Bundles scripts
No
Pre-approved tools
None declared
Needs
Nothing beyond the agent
Client-specific fields
None, spec fields only

Its instructions steer what your agent does, and it may have changed since the commit we read. How to check a skill.

Instructions and four reference files. The agent may fetch Cloudflare documentation pages to confirm an API, so it uses network access in agents that allow it. No scripts, no allowed-tools.

At commit 5108325 the SKILL.md is 68 lines long. Alongside it:

  • references/ (4 entries)

Alternatives

Agents SDK from the same repository when the Worker is an agent. Durable Objects for stateful coordination.

SKILL.md

The file exactly as published at the reviewed commit, reproduced under its Apache-2.0 licence.

skills/workers-best-practices/SKILL.md
---
name: workers-best-practices
description: Cloudflare Workers best practices for production applications. Use when writing, reviewing, or configuring Workers.
---

Your knowledge of Cloudflare Workers APIs, types, and configuration may be outdated. **Prefer retrieval over pre-training** when writing or reviewing Workers code.

Use the project's installed versions, generated types, and Wrangler compatibility settings as the baseline for existing code. Retrieve relevant Cloudflare documentation to verify API, configuration, runtime behavior, and limit claims.

## References

Read the sections relevant to the task:

| Reference | When to use it |
|-----------|----------------|
| [Configuration and observability](references/configuration.md) | Compatibility dates, bindings, generated types, secrets, logs, and traces |
| [Runtime patterns](references/runtime-patterns.md) | Streaming, promise lifetime, request state, service calls, security, and runtime tests |
| [Platform API checks](references/platform-apis.md) | Handler signatures, platform classes, binding access, and serialization |

For missing evidence, consult [Workers best practices](https://developers.cloudflare.com/workers/best-practices/workers-best-practices/index.md) or find the affected product in the [Cloudflare docs directory](https://developers.cloudflare.com/directory/index.md). Use the installed Wrangler schema for config fields. A newer type package does not supersede the project's configured target.

## Keep Compatibility Dates Current

Use today's date for new Workers. Encourage periodic updates for existing Workers, reviewing compatibility changes and running relevant tests. Assess existing behavior against its configured date and flags; see [compatibility guidance](references/configuration.md#keep-compatibility_date-current).

## Enable Observability

Enable [Workers Logs](https://developers.cloudflare.com/workers/observability/logs/workers-logs/index.md) and [Traces](https://developers.cloudflare.com/workers/observability/traces/index.md) when creating or preparing a Worker for production. Set `observability.enabled` and `observability.traces.enabled` to `true`; the top-level setting alone does not enable traces. Use structured JSON logging and configure sampling for the workload. During reviews, flag missing logs or traces. See the [configuration example](references/configuration.md#enable-workers-logs-and-traces).

## Place Compute Near Regional Databases

For Workers that make multiple sequential queries to one regional database, configure explicit placement matching the database's provider and region before deployment. Ask for missing location information, preserve existing placement choices, and verify handler eligibility and deployed request duration. See [database placement](references/configuration.md#place-compute-near-regional-databases).

## Anti-Patterns to Flag

| Anti-pattern | Consequence and preferred pattern |
|-------------|-----------------------------------|
| `await response.text()` or similar buffering on unbounded data | Can exhaust Worker memory; [stream large or unbounded bodies](references/runtime-patterns.md#stream-request-and-response-bodies). |
| Hardcoded secrets in source or config | Leaks credentials through version control; use Wrangler secrets. |
| `Math.random()` for security-sensitive tokens or IDs | Predictable values; use `crypto.randomUUID()` or `crypto.getRandomValues()`. |
| Async work started without awaiting, returning, or attaching it to `ctx.waitUntil()` | Work can be dropped and errors missed; tie it to the request or background-work lifetime. |
| Module-level mutable request state | Leaks data across requests and can cause I/O ownership errors; pass request state explicitly. |
| Cloudflare REST API calls for operations available through Worker bindings | Adds network and authentication overhead; use the available binding. |
| `ctx.passThroughOnException()` used as general error handling | Can conceal Worker failures by forwarding to the origin; use explicit error handling and structured error responses. |
| Hand-written `Env` that duplicates Wrangler bindings | Can drift from configuration; generate binding types with `wrangler types`. |
| Direct string comparison of secret values | Can expose timing differences; use the [Web Crypto comparison pattern](references/runtime-patterns.md#use-web-crypto-for-secure-token-generation). |
| Destructuring `ctx` methods, such as `const { waitUntil } = ctx` | Loses the receiver; call `ctx.waitUntil(...)`. |
| `any` on `Env` or handler parameters | Hides binding and handler contract errors; use the project's generated and platform types. |
| `as unknown as T` to force a platform type match | Hides incompatibilities; fix the underlying contract. |
| `implements` used in place of extending a platform base class | Does not inherit runtime behavior, `this.ctx`, or `this.env`; use the appropriate base class. |
| Unbound `env.X` in a platform class method | Bindings are available through `this.env.X`; see [binding access patterns](references/platform-apis.md#binding-access--the-most-common-error). |
| Applying one serialization rule across Queues, Workflow steps, storage, and WebSockets | Can reject valid payloads or accept unsupported ones; check the [specific API and encoding](references/platform-apis.md#serialization-boundaries). |

## Validation

Use the project's existing checks for affected Workers behavior: type-check binding or handler contract changes, and run relevant runtime tests for behavior changes. Preserve required repository checks; a narrow edit does not require a full Workers audit.

## Scope

This skill covers Workers-specific best practices and code review. For related topics:

- **Durable Objects**: load the `durable-objects` skill
- **Workflows**: see [Rules of Workflows](https://developers.cloudflare.com/workflows/build/rules-of-workflows/index.md)
- **CLI commands**: load the `cf-cli` skill, or the `wrangler` skill in a project with a Wrangler configuration file

## Skill feedback

If the user reports that this skill gave incorrect or outdated guidance, or is missing information, read [references/skill-feedback.md](references/skill-feedback.md).
  • Wrangler (Cloudflare)

    Cloudflare's skill for running and troubleshooting Wrangler in Workers projects: local dev, Previews, deploys and resources, using the project's own version.

    Checked at 5108325

  • Durable Objects (Cloudflare)

    Cloudflare's skill to build, debug and review Durable Objects: when to use them, wrangler config, storage, alarms, stubs, authorisation and testing.

    Checked at 5108325

  • Agents SDK (Cloudflare)

    Cloudflare's skill for building, debugging and reviewing agents on its Agents SDK: Agent classes, routing, state, scheduling, chat, MCP and the React client.

    Checked at 5108325