# Add Request ID Logging to a Hono Worker

Yes. An agent adds Hono's requestId and logger middleware to a Cloudflare Worker, echoes the id on responses and errors, adds Vitest cases, and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/add-request-id-logging/hono/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the Worker as it is rather than a template. In a project shaped like the `blog` workspace of honojs/examples it finds a `package.json` with `hono` at `^4.2.4`, `wrangler` at `^3.53.0` and Vitest pinned to 1.3.0 under `@cloudflare/vitest-pool-workers`, a `wrangler.example.toml` with `compatibility_date` of 2024-04-01, the `nodejs_compat` flag and a KV namespace bound as `BLOG_EXAMPLE`, and a `vitest.config.ts` that runs the tests inside workerd through `defineWorkersConfig`. It reads `src/index.ts`, where the root app mounts `prettyJSON` and a `basicAuth` check on non-GET requests under `/posts/*` before routing to `src/api.ts`, and `src/api.ts`, where five handlers read and write posts through the KV helpers in `src/model.ts` under the `v1:post:` prefix.

Nothing in those files identifies a request. The error path in `api.ts` returns `{ error, ok: false }` with no way to find the matching line in `wrangler tail`, and there is no logger at all. So the agent adds two pieces of middleware from Hono itself, no new dependency: `requestId()` from `hono/request-id`, mounted first on the root app in `src/index.ts` so it runs before `basicAuth` can reject a request, and `logger()` from `hono/logger` with a custom print function that prefixes each line with `c.get('requestId')`. If the installed Hono resolved by the caret range is older than the `hono/request-id` export, the agent bumps `hono` in that workspace's `package.json` and reruns `npm install` from the monorepo root, which is where the `npm@10.0.0` workspaces are managed.

The id has to reach the client too, or nobody can quote it in a bug report. `requestId` already sets `X-Request-Id` on the response and honours one sent by the caller, so the agent leaves that header alone and instead threads the id into the JSON error bodies: the 422 from `POST /posts`, the 404 from `GET /posts/:id`, and the root `notFound` handler in `index.ts` all gain a `requestId` field beside `ok: false`. It adds an `app.onError` to the root app that logs the error with the id and answers 500 with the same shape, since the example has no error handler of its own. The `Bindings` type in `src/bindings.ts` is untouched, because the id lives in Hono's per-request variables, not in the environment.

The tests run where the code runs. `src/api.test.ts` already has eight cases that call `worker.request(...)` with the test `env` from `cloudflare:test`, so the agent adds cases there: a request with no header gets an `X-Request-Id` back that looks like a UUID, a request that sends `X-Request-Id: abc-123` gets `abc-123` echoed, and the 404 body for an unknown post carries the same id as its header. It runs `npm test` in the workspace, which is `vitest --run` under the workers pool, and `npx wrangler deploy --dry-run` to be sure the bundle still builds, then opens a pull request with the diff and a sample log line in its description.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add request ID logging to this Hono Worker. Mount Hono's requestId middleware before every other middleware and its logger middleware with the id in each line, echo the id in the X-Request-Id response header and in every JSON error body including the notFound and onError handlers, add Vitest cases for a generated id, an echoed id and the id in a 404 body, run npm test and a wrangler dry-run deploy, and open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request on its own branch. The diff touches `src/index.ts` for the two `app.use` lines and the new `onError`, `src/api.ts` where each error response gains its `requestId` field, and `src/api.test.ts` with the three new cases. `package.json` changes only if Hono had to move forward to a version that exports `hono/request-id`. The description shows one logger line as it will appear in `wrangler tail`, with the id at the front, and names the header a client should send to correlate a retry with the first attempt.

The proof is in two places. The session's trajectory at `GET /o/{org}/sessions/{id}/messages` keeps every `npm test` run with Vitest's output, including the first run where the new cases fail before the middleware is in place. The changed files, with their diffs, are at `GET /o/{org}/p/{project}/sessions/{id}/changes`, so a reviewer can read the three touched source files without opening the branch.

A reviewer checks that `requestId()` is mounted on the root app and ahead of `basicAuth`, so a rejected login still gets an id; that the `X-Request-Id` a caller supplies is used rather than overwritten; and that the id never leaks into a `GET /posts` list body, only into headers and error bodies. Nothing is deployed: `wrangler deploy` runs with `--dry-run`, and the Worker on Cloudflare changes only when someone merges and deploys.

## Run it on every new Worker

A team that runs several Hono Workers starts one run per repository with `POST /o/{org}/sessions`, this prompt and that repository in `resources`. Each run reads its own `src/index.ts` and its own error handlers, so a Worker that already has an `onError` gets the id added to it rather than a second handler, and each opens its own pull request. Send a `request_id` in each body so a retried request returns the run it already started instead of a second one.

Set a `webhook_url` on the API key and every run the key starts posts `run.completed` or `run.failed` when it finishes, with a `session_url` for the result, so a script can collect the pull request links for review together. The key runs at most `max_concurrent` sessions at once, 4 by default, and a long list of Workers can be sent in one pass: the sessions past that answer `202` with `status: "queued"` and start as earlier ones end.

## Why run it on Rush?

On a laptop this is a half hour of context switching: open the Worker, remember which Hono export the middleware lives under, wire the id into four error paths, and run the Vitest workers pool, which needs `wrangler.toml` present even though the repository ships only `wrangler.example.toml`. On Rush the run has its own sandbox with a computer in it: the agent copies the example config into place for the tests, runs `npm test` until the three new cases pass, and your working tree stays on whatever you were doing.

The result is checkable rather than claimed. The pull request carries the diff, the trajectory keeps every test run and its output, and the changes endpoint shows the touched files, so a reviewer can confirm the header is echoed and the body shape is right without checking the branch out. Ten Workers mean ten sandboxes and ten pull requests at once, each isolated from the others.

## Frequently asked questions

### Does this add a dependency?

No. Both `requestId` and `logger` ship inside the `hono` package, under `hono/request-id` and `hono/logger`. The only time `package.json` changes is when the resolved Hono version predates the `hono/request-id` export, in which case the agent bumps `hono` in the workspace and reruns `npm install` from the monorepo root where the workspaces are declared.

### What if the client already sends a request id?

It is kept. Hono's `requestId` middleware reads the incoming `X-Request-Id` header and only generates a UUID when there is none, so an id from a gateway or a mobile client is the one that appears in the logs and the error body. One of the new Vitest cases sends `X-Request-Id: abc-123` and asserts the same value comes back.

### Will the id appear in successful responses?

In the header, yes: every response carries `X-Request-Id`. In the body, no: `GET /posts` and `GET /posts/:id` keep their shape, and the id is added only to error bodies, where a client needs something to quote. If you want it in every body, say so in the prompt and the agent adds it to the success responses in `src/api.ts` as well.

### How do the tests run without a real KV namespace?

The repository's `vitest.config.ts` uses `@cloudflare/vitest-pool-workers`, which runs the suite inside workerd with the bindings from `wrangler.toml`, so the `BLOG_EXAMPLE` namespace is a local one and the existing eight cases already create and delete posts in it. The new cases use the same `worker.request(path, init, env)` pattern the file already has.

### Does anything get deployed?

No. The agent runs `wrangler deploy --dry-run` to prove the bundle builds, which uploads nothing, and it has no Cloudflare credentials in the sandbox. The Worker on Cloudflare changes only when someone merges the pull request and runs the repository's own `npm run deploy`.

## More Hono jobs

- [Add Request Timeouts to a Hono App](https://getrush.ai/run/add-request-timeouts/hono/)
- [Add a Typed RPC Client to a Hono API](https://getrush.ai/run/add-rpc-client/hono/)
- [Add Security Headers to a Hono App](https://getrush.ai/run/add-security-headers/hono/)

## The same job on other stacks

- [Add Request ID Logging to a Koa App](https://getrush.ai/run/add-request-id-logging/koa/)
- [Add Request ID Logging to a Ktor Server](https://getrush.ai/run/add-request-id-logging/ktor/)
- [Add Request ID Logging to a Leptos App on Axum](https://getrush.ai/run/add-request-id-logging/leptos/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
