# Investigations

Ask questions of your own past orders, traffic and agent actions in plain words, and get an answer worked out by read-only queries you can read.

An investigation is a thread of questions about your own records: "which shipping countries had the most disputes
among orders we allowed", "what do the orders rule 4 declines have in common". A model works out each answer by
writing [analysis queries](https://provenanceauth.com/docs/account/analysis.md) over your records. You get a short answer, the table behind it and
the queries, so you can read exactly how it was worked out.

## When to use it

- A question about how your rules decided that a dashboard doesn't answer.
- Looking for what the orders a rule missed, or declined wrongly, have in common.
- Finding a pattern worth testing as a new rule, then sending it to Suggestions.

## How it works

1. **Turn it on.** Investigations are off until a member with the Manage policies permission turns them on, after
   reading what is sent to the model: `PUT /v1/account/investigations/settings` with `on` and the current
   `notice_version`, which `GET /v1/account/investigations/settings` returns along with whether this deployment can
   run Investigations. The console's Investigations tab and its settings are on the way.
2. **Ask.** `POST /v1/account/investigations` starts one; a `question` in the body is answered at once. Ask the next
   one at `POST /v1/account/investigations/{id}/messages`.
3. **The model writes queries.** It reads your records only by writing read-only SQL over the views in the
   investigation's scope. Each question may take at most 12 tool calls, 60 seconds and a token budget.
4. **The answer is checked.** Every figure in the answer's text must be a value in one of this question's query
   results. If the model states a figure no query returned, it is asked once to correct it; if it still doesn't
   match, the answer shows the tables alone with "The summary couldn't be checked against the queries."
5. **You read it.** The answer streams as server-sent events: `query` as each query runs, `delta` with the answer's
   text, then `answer` (the whole message) and `usage`. Send `Accept: application/json` to get one JSON body instead.

One question is answered at a time in an investigation, and three at a time across your account.

### What it may read

The scope chooses the views an investigation may read; all three are in it by default.

| View | Records | Key scope |
| --- | --- | --- |
| `orders` | Order decisions with the outcomes you reported | `authorize` |
| `traffic` | Traffic decisions | `identify` |
| `trace_evals` | Trace assessments of your agents' actions | `trace` |

A view left out of the scope isn't shown to the model, and a query that names it is refused. An API key reads only
the views its scopes cover.

### What an answer holds

| Part | What it is |
| --- | --- |
| The answer | One or two sentences, every figure checked against a query result |
| The tables | The rows of the query results, drawn from the stored results, never from the model's text |
| The queries | Every query as it ran, with its window, row count and time; one that couldn't run says so, and no figure is given for it |
| Findings | Patterns the model drafted as a candidate rule, signal, scorecard or risk score; nothing is created until you send one to Suggestions |
| The cost | What the answer cost, in dollars |

The result is `ok`; `stopped` when a limit was reached, so the tables show what the queries found so far; `unchecked`
when the answer's text was dropped by the figure check; or `unavailable` when the model couldn't answer and no query
result came back.

### Send a finding to Suggestions

`POST /v1/account/investigations/{id}/messages/{message_id}/findings/{finding_id}/suggest` sends a finding to
Suggestions, where it is tested on 90 days of your labelled records before anyone sees it: kept as a suggestion marked
"From an investigation", or dropped with the reason. Until Suggestions is available on your account, a sent finding is
held (`status: held`) with its draft kept, and it is tested when Suggestions arrives. A finding never becomes a draft
rule by itself; only a person adds a suggestion to a draft.

### Open the records

`GET /v1/account/investigations/{id}/messages/{message_id}/records?query=2` returns the record ids of one query's
result, which the console opens as a filtered list. Only a result that selected the view's `id` column has them; a
count or a sum says so.

### What is sent to the model

- Your account's records are read only through analysis queries, each in a sandbox that holds only your rows, as a
  read-only role. An investigation can't read another account's records or change anything.
- Personal columns, such as a billing postal prefix or a request path, are never shown to the model and can't be
  queried from an investigation.
- Sent to the model provider named in the settings: the question and the earlier turns, the views' columns, your live
  rules' names and labels, and the query results, at most 200 rows a query, with text cut to 80 characters and marked
  as data.
- Questions, answers, queries and their results are stored encrypted and kept 90 days from the investigation's last
  activity. Delete one at any time with `DELETE /v1/account/investigations/{id}`.

An answer is a reading of your own records, with the queries shown. It isn't a statement that anything is true beyond
what those queries return.

### What it costs

Each answer costs the model's list price for the calls it took plus 10%, shown on the answer in dollars and billed as
`investigation_usage` ([plans and usage](https://provenanceauth.com/docs/account/plans-and-usage.md#investigations)). No amount is included. Free and trial accounts
have a monthly cap, shown in the settings (you can set less); at the cap a question answers
`investigation_over_budget`. Paid plans have no cap, and the account owner is emailed when the month's spend first
passes each notice amount.

### Who can use it

A signed-in member with the Manage policies permission, or an account API key. Each question records who asked.

### Errors

| Code | When |
| --- | --- |
| `investigation_off` (403) | Investigations aren't on for your account. |
| `investigation_unconfigured` (503) | This deployment can't run them yet; the message names the missing setting. Nothing was sent to a model. |
| `investigation_over_budget` (402) | A Free or trial account reached its monthly cap. |
| `investigation_busy` (429) | A question is already being answered in this investigation, or three across your account. |
| `resource_missing` (404) | No such investigation, answer or finding on your account (`param` names which id). |

## Related

- [Analysis queries](https://provenanceauth.com/docs/account/analysis.md)
- [AI assists](https://provenanceauth.com/docs/account/ai-assists.md)
- [Errors](https://provenanceauth.com/docs/platform/errors.md)
