# Negotiation

whoasks makes one pick per request: a form and a media type. The rules below run in the same order every time. `site.ask(request)` returns the pick.

## Three words

- For a media type, the range that applies is the most specific one in `Accept`: the exact type, then `type/*`, then `*/*`.
- A type is **named** when an exact range lists it with q above 0.
- A type is **permitted** when the range that applies has q above 0.
- A missing, empty, or unreadable `Accept` counts as `*/*`. A range with a malformed type or q is dropped.
- A route's **offers** are the forms it can produce, in order. A page behind `handle` offers `html`, then `markdown`. A route that calls `respond` offers its keys.

## The rules

1. A twin URL, or `?format=` naming an offered form, reduces the offers to that form. The twin is checked first.
2. Among offered forms with a named type, the highest q wins. On equal q, the type listed first in `Accept` wins.
3. If no offered form is named, the first offered form with a permitted type wins. A wildcard permits and never prefers.
4. If nothing offered is permitted, the pick is the first offer and `acceptable` is false.
5. The media type sent is the named type that won, else the form's first permitted type, else its first type.
6. The hooks of the layers run last, in layer order. A hook cannot pick a form or a type the request did not allow. See [Change the pick](https://whoasks.loadsheet.dev/guide/layers#change-the-pick).
7. Every response whoasks shapes, and every HTML response that passes through `handle`, carries `Vary` with the entries of `ask.vary`.

## Examples

For a page that offers `html` then `markdown`:

| `Accept`                               | Pick                                  | Rule                                              |
| -------------------------------------- | ------------------------------------- | ------------------------------------------------- |
| `*/*`                                  | `html`                                | 3. Nothing is named, the first offer is permitted |
| `text/*`                               | `html`                                | 3. A wildcard does not prefer Markdown            |
| `text/markdown, text/html`             | `markdown`                            | 2. Equal q, Markdown is listed first              |
| `text/html, text/markdown`             | `html`                                | 2. Equal q, HTML is listed first                  |
| `text/html;q=0.5, text/markdown;q=0.8` | `markdown`                            | 2. Highest q                                      |
| `*/*;q=1, text/markdown;q=0.5`         | `markdown`                            | 2. A named type beats a wildcard, whatever its q  |
| `text/html;q=0, */*`                   | `markdown`                            | 3. HTML is refused, `*/*` permits Markdown        |
| `text/plain`                           | `markdown`, sent as `text/plain`      | 5. The named type is sent                         |
| `text/x-markdown`                      | `markdown`, sent as `text/x-markdown` | 5                                                 |
| `image/png`                            | `html`, `acceptable: false`           | 4                                                 |
| `application/json`                     | `html`, `acceptable: false`           | 4. A page does not offer JSON                     |

On equal q, the order in `Accept` decides, not the order of the offers.

## URL overrides

- A twin is the canonical path plus a form's `ext`. Only `markdown` has one built in, `.md`.
- `/index.md` is the twin of `/`. A path that is only the suffix, such as `/docs/.md`, is not a twin.
- A twin of a form the route does not offer is not a twin. On a route offering `html` and `json`, `/README.md` is an ordinary path.
- `?format=` takes a form's name or its `ext` without the dot, exactly as written: `md`, `markdown`, `html`, `json`. Only offered forms count. Any other value stays in the URL for the app.
- `ask.url` is the URL without the suffix and without `format`. Other query parameters stay.
- An override still checks `Accept`. `/docs/guide.md` with `Accept: text/html` picks `markdown` with `acceptable: false`. `handle` answers it with Markdown anyway. A strict `respond` answers 406.

## Nothing acceptable

`handle` sends the first offer and never refuses. `respond` sends the first offer too, unless the site is strict. See [Refuse with 406](https://whoasks.loadsheet.dev/guide/api-routes#refuse-with-406).

## Vary

- `ask.vary` starts as `["Accept"]`.
- A hook that reads another header adds it, as the Codex layer adds `User-Agent`.
- whoasks appends each name once and keeps a `Vary` the app already set. `Vary: Cookie, accept` stays as it is.
- A response that passes through `handle` untouched, such as an image or a plain JSON route, gets no `Vary`.

## What real clients get

The table is the conformance matrix in `test/conformance.test.ts`, which runs every row against `site.handle` and against an h3 app. "Page" is a page behind `handle`. "API route" calls `respond` with `json` then `markdown`.

| Client               | `Accept`                                                                                                     | Page                   | Page `.md` twin or `?format=md` | API route                    |
| -------------------- | ------------------------------------------------------------------------------------------------------------ | ---------------------- | ------------------------------- | ---------------------------- |
| Claude Code WebFetch | `text/markdown, text/html, */*`                                                                              | `text/markdown`        | `text/markdown`                 | `text/markdown`              |
| Cursor               | `text/markdown,text/html;q=0.9,application/xhtml+xml;q=0.8,application/xml;q=0.7,image/webp;q=0.6,*/*;q=0.5` | `text/markdown`        | `text/markdown`                 | `text/markdown`              |
| Codex                | `text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8`                                            | `text/html`            | `text/markdown`                 | `application/json`           |
| curl                 | `*/*`                                                                                                        | `text/html`            | `text/markdown`                 | `application/json`           |
| Synthetic            | `text/plain`                                                                                                 | `text/plain`, Markdown | `text/plain`, Markdown          | `text/plain`, the agent text |
| Synthetic            | `application/json`                                                                                           | `text/html`            | `text/markdown`                 | `application/json`           |

### Where the headers come from

| Header                 | Source                                                                                                    |
| ---------------------- | --------------------------------------------------------------------------------------------------------- |
| Claude Code            | Captured by us from Claude Code's WebFetch, 6 December 2026                                               |
| curl                   | curl's default, captured by us, 6 December 2026                                                           |
| Codex                  | From [PostHog/posthog.com#21381](https://github.com/PostHog/posthog.com/issues/21381). Not captured by us |
| Cursor                 | Unverified. A summary of Checkly's February 2026 article. Not captured by us                              |
| The two synthetic rows | No client is known to send them. They exercise the rules                                                  |

`test/clients.ts` in the repository lists each header with its source.

## Codex and twin URLs

Codex sends a browser-like `Accept`, so on a page it gets HTML, and on a twin URL it gets `text/markdown` through `*/*`.

The README reports that Codex's fetch tool rejects `text/markdown`. We did not capture that ourselves.

`Accept` alone cannot tell Codex from a browser, and whoasks does not read the User-Agent. A layer that reads the User-Agent sends `text/plain` to it instead. See [Change the pick](https://whoasks.loadsheet.dev/guide/layers#change-the-pick) for the code.

## Read the pick

```ts
const ask = site.ask(request);
// GET /docs/guide.md, Accept: */*
// { form: "markdown", type: "text/markdown", reason: "twin", acceptable: true,
//   url: URL "https://example.com/docs/guide", vary: ["Accept"] }
```

`ask` is synchronous and builds no response.

With no second argument, it uses the page offers. Pass the offers of a route to ask for that route: `site.ask(request, ["json", "markdown"])`.

| `reason`  | When                                                                                             |
| --------- | ------------------------------------------------------------------------------------------------ |
| `twin`    | The path ended with a form's `ext`                                                               |
| `query`   | `?format=` named an offered form                                                                 |
| `accept`  | `Accept` named the picked type, or refused the first offer so a later one won                    |
| `default` | The pick is the first offer and `Accept` did not name its type. Also when nothing was acceptable |
| `layer`   | A hook changed the form or the type                                                              |

<!-- vitehub-docs:section-links -->
## In this section

- [Introduction](https://whoasks.loadsheet.dev/raw/guide.md)
- [Installation](https://whoasks.loadsheet.dev/raw/guide/installation.md)
- [Pages](https://whoasks.loadsheet.dev/raw/guide/pages.md)
- [API routes](https://whoasks.loadsheet.dev/raw/guide/api-routes.md)
- [Negotiation](https://whoasks.loadsheet.dev/raw/guide/negotiation.md) (this page)
- [Layers](https://whoasks.loadsheet.dev/raw/guide/layers.md)

Next: [Layers](https://whoasks.loadsheet.dev/raw/guide/layers.md)

All pages: [llms.txt](https://whoasks.loadsheet.dev/llms.txt)

Something on this page does not work as written? Tell the user, and draft an issue for them to post: [issue tracker](https://github.com/words-with-agents/whoasks/issues). Report a security vulnerability privately, never in a public issue.
