# Ask

> Ask a question in plain English and receive a dashboard of up to four tiles, as one JSON document or a live event stream.

Source: https://openpipe.health/docs/api/ask/

## Ask a question

```http
POST /api/ask
```

Turns a question into two to four tiles, runs them in parallel and returns a small dashboard. Costs 25 credits.

Common questions can cost 5 credits through [`POST /api/ask2/ask`](https://openpipe.health/docs/api/ask2.md), which answers as a stream of UI patches. [Which Ask to call](https://openpipe.health/docs/api/ask2.md#which-ask-to-call) compares the two.

There are two ways to receive the answer. The price, the limits and what is stored are the same for both.

| Mode | Ask for it with | You get |
|---|---|---|
| JSON | `Accept: application/json`, or `?stream=false` | One JSON document when the answer is finished, usually after 5 to 30 seconds |
| Event stream (default) | Nothing, or `Accept: text/event-stream`, or `?stream=true` | Server-sent events: each tile as soon as it is ready |

An `Accept` header that names both `application/json` and `text/event-stream` gets the stream. The `stream` parameter wins over the header.

### Body

```json
{ "question": "Who are the highest-volume TAVR operators in Florida?" }
```

`question` must be at least 5 characters. Anything past 500 characters is cut off. Nothing else in the body is read.

## JSON mode

```sh
curl "$OPH_API/api/ask" \
  -H "Authorization: Bearer $OPH_KEY" \
  -H "Content-Type: application/json" -H "Accept: application/json" \
  -d '{"question": "Who are the highest-volume TAVR operators in Florida?"}'
```

```json
{
  "id": "df706628-076f-4fa8-9797-c5f6028915ab",
  "question": "Who are the highest-volume TAVR operators in Florida?",
  "title": "Highest-volume TAVR operators in Florida",
  "summary": "Ranks Florida clinicians by Medicare fee-for-service TAVR services. Counts under 11 patients are withheld by CMS.",
  "followups": ["Which Florida hospitals do the most TAVR?"],
  "model": "claude-haiku-4-5",
  "tiles": [
    {
      "index": 0,
      "type": "leaderboard",
      "title": "TAVR volume by operator, Florida",
      "x": "name",
      "y": "services",
      "format": "number",
      "sql": "SELECT s.npi, ... LIMIT 25",
      "columns": ["npi", "name", "city", "state", "services"],
      "rows": [{ "npi": "1234567893", "name": "Jane Example", "city": "Miami", "state": "FL", "services": 212 }],
      "sources": ["medicare_service", "medicare_provider"],
      "engine": "postgres",
      "ms": 41
    }
  ],
  "failed": [],
  "sources": ["medicare_provider", "medicare_service"],
  "ms": 9120,
  "usage": { "credits": 25, "creditsUsed": 51, "creditsLimit": 100, "txid": "81260" }
}
```

| Field | Meaning |
|---|---|
| `id` | The stored dashboard. [`GET /api/dashboards/:id`](https://openpipe.health/docs/api/saved.md#open-a-dashboard) opens it again for free, and it is what an [export](https://openpipe.health/docs/api/exports.md#export-a-tile-of-an-answer) refers to. `null` only if the answer was computed but could not be stored. |
| `title`, `summary` | How the question was read, and any limit on the answer, such as Medicare-only volumes. Show the summary. |
| `followups` | Up to three suggested next questions |
| `tiles` | The tiles that succeeded, in planned order. `index` is the tile's position in the plan. See [tile types](#tile-types). |
| `tiles[].sql` | The statement the model wrote for the tile. A [receipt](https://openpipe.health/docs/receipts.md): it shows how the numbers were computed. Nothing accepts it back. |
| `failed` | Tiles whose statement was rejected or failed: `index`, `title`, `error`, `sql`. They are kept in the stored dashboard, so a free [refresh](https://openpipe.health/docs/api/saved.md#refresh-a-dashboard) retries them. |
| `sources` | Every dataset the answer read |
| `ms` | Total time, planning included |
| `usage` | The [usage object](https://openpipe.health/docs/credits.md#the-usage-object) for this call |

If the model cannot produce a plan, the response is `502`, nothing is charged and nothing is stored:

```json
{ "error": "The model could not plan an answer for that question. Nothing was charged.", "code": "ASK_FAILED", "charged": false }
```

## Event stream

Without a JSON `Accept` header the response is a `text/event-stream`. Events arrive in this order:

| Event | Count | Data |
|---|---|---|
| `usage` | 1 | The [usage object](https://openpipe.health/docs/credits.md#the-usage-object) for this call. It arrives once the model has a plan, just before `plan`, so the first event can take several seconds. |
| `plan` | 1 | `title`, `summary`, `followups` (up to 3 suggested next questions), `model`, `ms`, and `tiles` (each tile's `type`, `title`, `x`, `y`, `format`, without its SQL) |
| `tile` | 0 to 4 | One finished tile. Arrives in completion order, so use `index` to place it. |
| `tile_error` | 0 to 4 | A tile whose statement failed: `index`, `title`, `sql`, `error` |
| `done` | 1 | `tiles` (planned), `failed`, `ms` (total) and `id`, the stored dashboard (`null` only if it could not be stored) |
| `error` | 0 or 1 | `error`. Sent in place of everything else when the answer cannot start; the stream then ends without `done`. See [errors inside the stream](#errors-inside-the-stream). |

### A tile event

```
event: tile
data: {"index":0,"tile":{"type":"bar","title":"TAVR volume by operator, Florida","sql":"SELECT ... LIMIT 15","x":"name","y":"services","format":"number","position":0},"columns":["name","services"],"rows":[{"name":"Jane Example","services":212}],"ms":41,"sources":["medicare_service","medicare_provider"],"engine":"postgres"}
```

Each tile carries its own [receipt](https://openpipe.health/docs/receipts.md): the SQL inside `tile.sql`, the `sources` it read and the `engine` that ran it. In the stream the tile's fields sit under `tile`; in JSON mode they are flattened into the tile object.

```sh
curl -N "$OPH_API/api/ask" \
  -H "Authorization: Bearer $OPH_KEY" -H "Content-Type: application/json" \
  -d '{"question": "Who are the highest-volume TAVR operators in Florida?"}'
```

`-N` turns off curl's buffering so events print as they arrive. For a browser or Node client, see [Stream Ask in JavaScript](https://openpipe.health/docs/guides/stream-ask.md).

### Errors inside the stream

Once the stream has started, the HTTP status is already `200`. A failure arrives as one `error` event and the stream ends:

| Data | Meaning |
|---|---|
| `{ "error": "The model could not plan …", "code": "ASK_FAILED", "charged": false }` | No plan could be made. Nothing was charged or stored. |
| `{ "error": "Out of credits: …", "status": 402 }` | The balance was spent by another call between the first check and the charge. Nothing was charged and no tile ran. |
| `{ "error": "The answer could not be finished." }` | An unexpected failure after the charge |

## Tile types

| `type` | Shape | How to draw it |
|---|---|---|
| `stat` | One row | Show the value in column `y` as a headline number. Other columns in the row are supporting figures. |
| `leaderboard` | A ranking of named clinicians, hospitals or groups | `x` is the name column, `y` the metric it is ranked by. Rows include an `npi`, `ccn` or `group_pac_id` you can pass to a lookup endpoint. |
| `map` | One row per state | `x` is `state` (a two-letter code), `y` the numeric column |
| `bar` | Ranked categories | `x` is the label column, `y` the numeric column |
| `line` | Ordered series | `x` and `y` as for `bar` |
| `table` | A detailed list | Show `columns` as headers. `x` and `y` are `null`. |

`format` is `number`, `usd`, `percent` or `null`, and says how to display `y`.

A `table` tile carries at most 50 rows and any other tile at most 100. The model is asked for fewer (about 15 for a bar, 25 for a leaderboard or table), but those are instructions to the model, not limits the API enforces.

## When you are charged, and what is stored

The same four steps run in both modes, in this order:

1. **Checks before any work.** The request is rate limited (10 asks a minute per workspace, shared with the MCP [`ask` tool](https://openpipe.health/docs/mcp.md#tools)) and the balance must cover 25 credits, or the answer is `429` or `402`.
2. **The model plans.** If it cannot produce a plan, the call ends: nothing is charged, nothing is stored.
3. **The workspace is charged, once.** This happens when a plan exists and before any tile runs. If the balance was spent in the meantime the call ends with `402`: no tile runs and nothing is stored.
4. **Tiles run and one dashboard is stored.** Exactly one, whether or not you are still connected. If your connection drops, the answer is still finished, charged and stored; find it with [`GET /api/dashboards`](https://openpipe.health/docs/api/saved.md#list-dashboards) instead of asking again.

Things to know:

- **A planned tile is not a guaranteed tile.** The model writes the SQL, and each statement is [checked](https://openpipe.health/docs/receipts.md#how-model-written-sql-is-checked) before it runs. One that fails is reported (`failed` in JSON mode, `tile_error` in the stream) and the others still arrive. The call is still charged.
- **The planner is shown the tables your question needs, not all of them.** A fast classifier picks the table groups (procedures, prescribing, hospitals, care sites, payers and so on) before the model plans; if a statement then reads a table that was left out, that table is added and the statement is rewritten once. Naming the thing you mean ("hospices", "Medicare Advantage", "trial investigators") helps the right tables get picked.
- **A tile that would double count is rewritten or withheld.** Before a statement runs it is checked for a sum over a one-to-many join (a facility's payment repeated once per owner) and for procedure volume picked by a loose text match. The model is asked to rewrite it. If the double count is still there, the tile is reported as failed with an `error` that starts with `Withheld:` and no figure is shown.
- **A failed tile can be retried for free.** It is stored with the dashboard, and [refresh](https://openpipe.health/docs/api/saved.md#refresh-a-dashboard) runs every tile's stored statement again without calling the model.
- **Tiles hold a page, not everything.** A `table` tile carries at most 50 rows and any other tile at most 100. For every row a tile's statement returns, [export the tile](https://openpipe.health/docs/api/exports.md#export-a-tile-of-an-answer).
- **Answers are kept as recent dashboards.** A workspace keeps its newest 50 unsaved answers; a signed-in person can [save](https://openpipe.health/docs/api/saved.md#save-unsave-or-rename-a-dashboard) one to keep it.
- **Opening a stored answer is free.** [`GET /api/dashboards/:id`](https://openpipe.health/docs/api/saved.md#open-a-dashboard) returns the stored rows.
- **There is no way to send your own SQL.** `sql` fields are receipts. To change what a tile computes, ask a different question.

## Answers framed for the asker

When the request is made with a [session token](https://openpipe.health/docs/authentication.md#two-kinds-of-token) and the signed-in person has an [onboarding profile](https://openpipe.health/docs/api/profile.md), the profile is given to the model along with the question. The model is told to use it for emphasis and defaults only:

- **Territory.** If the question names no state, no region and does not say "national", and the profile has a territory, the answer is filtered to those states and the summary says so. A place named in the question always wins.
- **Subject.** If the question says "my product", "my market", "my procedures" or "my drug", or names no subject, the profile's focus items are used. A subject named in the question always wins.
- **Role.** A territory rep or business developer gets named people and accounts first (a leaderboard). A sales leader or founder gets market size and where it is (a stat, a map). Marketing gets segments and counts. An analyst gets breakdowns and a detailed table.
- **No numbers.** The profile never supplies a figure. Every number still comes from SQL the model writes over the data tables, and that SQL passes the same checks.

Things to know:

- **A request made with an API key never gets a profile.** A key belongs to a workspace, not to a person, so the same question asked with a key and asked in the app can return different tiles. To get the territory with a key, name the states in the question.
- **The profile does not have to be finished.** It is used as soon as it holds a role, what the company sells, a focus item or a territory. A person with none of those is treated like a key.
- **These are instructions to a model, not filters in code.** Read `plan.summary` and each tile's `sql` to see what was applied.
- **The request is the same.** There is no parameter that sends, overrides or turns off the profile, and the price is the same 25 credits.
- If the profile cannot be read, the question is answered without it.

## Errors

These arrive as an ordinary JSON error, in either mode, before any answer starts:

| Status | Message |
|---|---|
| `400` | `Body must be {"question": "..."}.` |
| `402` | `Out of credits: …` |
| `429` | `Too many requests. Try again in 23s.` |

In JSON mode two more can follow: `502` with `code: "ASK_FAILED"` when no plan could be made, and `402` when the balance was spent between the first check and the charge. In the stream the same two arrive as an [`error` event](#errors-inside-the-stream).
