Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 151 additions & 0 deletions specs/014-openai-reasoning-effort/research.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# Research: OpenAI reasoning effort (2026-11-03 to 2026-11-05)

These notes cover the installed libraries, langchain-openai 1.6.2 and openai 2.54.0,
and API facts measured on 2026-11-03 with the project's key using tiny requests
(about 150). The R numbers are cited from code (`src/agent/graph.py`,
`src/util/config_yml/models.py`) and are kept stable.

## R1 — What the API accepts (measured 2026-11-03)

gpt-6-luna:

| Request | Result |
|---|---|
| `temperature=0.0` | 400: "Only the default (1) value is supported." |
| `temperature=1.0` | accepted |
| `reasoning_effort` `none` / `low` / `medium` / `high` / `xhigh` | accepted |
| `reasoning_effort=minimal` | 400, listing the five above |
| the three graders (`json_schema`) at `reasoning_effort=medium` | valid output, one question each |
| streamed, with `stream_usage` | text chunks, and usage reports `output_token_details.reasoning` |

Across the 38 chat models `models.list()` returned (dated snapshots counted with their
base model), one request each at `medium`:

- gpt-4o, gpt-4o-mini, gpt-4.1 and gpt-4.1-mini reject it ("Unrecognized request
argument supplied: reasoning_effort").
- gpt-3.5-turbo, gpt-4, gpt-4-turbo, gpt-4.1-nano, o1, o3-mini, o4-mini and the
dated `*-chat-latest` models returned 404 (deprecated).
- Every other model accepted it. The levels each accepts:

| Model | none | minimal | low | medium | high | xhigh |
|---|---|---|---|---|---|---|
| chat-latest | – | – | – | ✓ | – | – |
| gpt-5, gpt-5-mini, gpt-5-nano | – | ✓ | ✓ | ✓ | ✓ | – |
| gpt-5.1 | ✓ | – | ✓ | ✓ | ✓ | – |
| gpt-5.2 to gpt-5.5, gpt-5.6-*, gpt-6-luna, gpt-6-sol, gpt-6.1-terra | ✓ | – | ✓ | ✓ | ✓ | ✓ |
| gpt-6-astra, gpt-6.1-sol, o3 | – | – | ✓ | ✓ | ✓ | ✓ |

Only `medium` is accepted everywhere. `low`, the non-answer default since 2026-11-05,
is accepted by every model except chat-latest.

A 16-token output cap made seven of these requests fail with a 400 that had nothing
to do with `reasoning_effort`. All seven passed when rerun with a 2048-token cap. So
a small cap is not a reliable way to probe a reasoning model.

## R2 — Sending no temperature

`ChatOpenAI(temperature=None, reasoning_effort=...)` leaves `temperature` out of the
request on 1.6.2. The old note in `get_llm`, "there is no way to send no temperature",
was true of 0.2.14 and became false with the 1.x upgrade (`4e607c8`).
`reasoning_effort` on its own keeps the request on `/v1/chat/completions`, which the
graders' `json_schema` method and the `astream_answer` streaming boundary both rely on.

As a side effect, this fixes a mismatch that was never reported. The library drops
any temperature other than 1 for `gpt-5*` models, so the `0.0` the table chose for
gpt-5.1, 5.2 and 5.4 was never actually sent.

## R3 — One rule per provider, no table

| Provider | Temperature | Reasoning effort |
|---|---|---|
| `openai` | none; a configured one or `LLM_TEMPERATURE` is refused | the configured level, otherwise `medium` for answer calls and `low` for the rest |
| `ollama` | `LLM_TEMPERATURE`, else configured, else `0.0`, as before | none; either key configured is refused |

Rejected alternatives:

- A per-model table of accepted levels. It would need editing for every new model,
and would not check models missing from it.
- A hand-kept list of the older models to refuse. R4 catches them without a list.

## R4 — The startup request

`check_model` sends `"Reply with the single word OK."` through the same model object
the calls will use, so the parameters are exactly theirs. It has no output cap (R1).
A rejection from the API (any status other than 429 and 5xx) stops the process. The
message quotes the provider's error, and for a rejected level that error lists the
accepted ones. It then picks a fix from the error's `param` and `code` when the API
sends them, and from its text otherwise:

- "Unrecognized" or `unsupported_parameter` `reasoning_effort`: the model takes
none, so choose a current one.
- Any other `reasoning_effort` error: set the key for that kind of call to a level the
model accepts.
- Anything else: check `OPENAI_API_KEY`, `LLM_BASE_URL` and the model name.

No answer is not a rejection: a connection error or timeout, a 429, or a 5xx, after
the client's own retries. These say nothing about the settings, so they are logged
as a warning and startup continues. Refusing would keep the server down through a
provider outage (revised 2026-11-09; it used to refuse on any failure).

A refusal is a `ModelConfigError`, not a `SystemExit`. `AgentGraph` and the
command-line tools turn it into an exit at startup.

It runs once per process, before slow work:

- **The server**: in `AgentGraph.__init__`, before the roughly 85 seconds of BM25
indexing. It checks the answer model only if its parameters differ.
- **The command-line tools**: the evaluator (each model and the judge), the routing
probe and the retrieval baseline each check when they start.
- **Not checked**: `_summary_llm`, which runs inside a request and builds from
`AgentGraph.answer_model`, the answer model startup already resolved and checked;
and Ollama.

It costs one short request per process start, two when the two levels differ.

Rejected alternatives: checking on the first question, which makes a user find the
failure, and an off switch, which would end up off on the one deployment where it
mattered.

## R5 — One decision for every place that builds a model

`resolve_model_parameters` decides what to send and does no network I/O.
Each caller resolves once and passes the result to `build_llm`, which builds and runs
R4 when asked. Resolving once means `AgentGraph` logs and compares the same parameters
it builds with. `build_llm` and `get_llm` both take the result as a required argument,
so a caller that skips the decision fails type checking.

| Site | Model | `llm:` config | Checked |
|---|---|---|---|
| `AgentGraph.__init__` (two models) | deployment's | yes | yes |
| `_summary_llm` (answer model) | deployment's, from `AgentGraph.answer_model` | yes, as resolved at startup | no |
| evaluator, model under test (two models) | `--model`, else the default | yes | yes |
| evaluator, judge | `--judge-model`, else gpt-6-astra | no | yes |
| routing probe | deployment's (was the default, at `0.0`) | yes (was no) | yes |
| retrieval baseline, self-query | default (was gpt-4o-mini) | no | yes |

The judge reads no config because a deployment's levels are chosen for its answering
model, and may be ones gpt-6-astra rejects (`none`, R1). The baseline reads none so
that its results don't move when one deployment's config changes. The answer sweep
also ignored `config.yml`, and was fixed here.

## R6 — Streaming with a reasoning model

`astream_answer` decides where the answer starts by the order of events, not by
anything about the model. On chat completions a reasoning model streams plain text
(R1), so the code did not change. It was not checked end to end on gpt-6-luna.

## R7 — Measurement (not kept)

`src/evaluation/answer_cost.py` was written to record latency and per-call tokens on
the served path, with prices passed in and a check that each call was sent the right
level. It was left out of the repository on 2026-11-09 without being run. What
remains is `astream_answer`'s `callbacks` argument. The chat's own
`OpenAICallbackHandler.total_cost` was not usable, since it records $0 for models
missing from its price table.

## R8 — The evaluator's judge

The default judge changed from gpt-4o, which rejects a reasoning effort (R1), to
gpt-6-astra, which is not the answering default, so the "judge is under test" guard
still holds. The evaluator's default `--model` is `DEFAULT_LLM_MODEL`, not a model
name written into the code. Scores from the two judges cannot be compared.
161 changes: 161 additions & 0 deletions specs/014-openai-reasoning-effort/spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# Feature Specification: OpenAI reasoning effort

**Feature Branch**: `llm-migration`

**Created**: 2026-11-03

**Status**: Built on `llm-migration`, 2026-11-03 to 2026-11-05, and not yet merged. The
spec was revised on 2026-11-05 from one reasoning effort for every call to two: `low`,
and `medium` for the answer. The startup refusals were checked against the API before
that revision. Nothing else was: the sweep, the completeness grader, the streaming path,
the command-line tools and the live-data loop were all left unchecked. The cost
measurement (Story 5) was not kept. The requirements below were set for that work, not
for later changes. Current behaviour is in `resolve_model_parameters`, `check_model` and
`build_llm` (`src/agent/graph.py:133-330`) and in `src/util/config_yml/models.py:41-55`.

**Input**: Request, 2026-11-03: "OpenAI models no longer accept temperature; send
reasoning effort instead, default medium; retire the temperature table and its probe."
Revised 2026-11-05: "I want the default llm reasoning effort to be low and only use
medium for the final answer generation step."

## Context (2026-11-03)

Spec 003 made temperature a measured fact about each model. A table, filled in by a
probe script, recorded which models accept `0.0` and which accept only `1.0`, and
every call sent a temperature from it.

That stopped being true. Current OpenAI models accept only their own default
temperature, so there is nothing to choose, and sending none gets the same default.
They take a reasoning effort instead. gpt-4o, gpt-4o-mini, gpt-4.1 and gpt-4.1-mini
are still being served while they are retired, and they reject any reasoning effort
(measured, [research](./research.md) R1). The code sent `0.0` to every model the table
did not list, gpt-6-luna included, so gpt-6-luna failed on a user's first question
rather than at startup.

Which levels a model accepts varies by model. Only `medium` is accepted by all of them.
Rather than keep a second hand-made table, the API is asked once at startup (R4).

The built-in default model changes to gpt-6-luna. That was decided before this spec;
the spec covers making it work.

A question makes several model calls, all on the answering model: rephrase, safety,
language, intent, query expansion, the answer, and, with post-processing on, the
completeness grader. Since 2026-11-05 the answer runs at `medium` and every other call
at `low`. The other calls return short or structured results, and they all run before
the first answer token, so reasoning spent on them is time the user waits through.

## User stories (set 2026-11-03, revised 2026-11-05)

### User Story 1 — Answer with an OpenAI reasoning model (P1; built)

With no `llm:` section, a question is answered by gpt-6-luna. No call sends a
temperature. The answer is sent `medium`, and every other call `low`.

**Check used**: offline, on the request each model would send (no `temperature` key,
the right `reasoning_effort`). The answer sweep, the completeness grader and the
streaming path were not run on the API.

### User Story 2 — A setting that cannot be honoured stops startup (P1; built)

Each of these makes the server exit before it accepts traffic, with a message naming
the setting, where it came from, and the fix: a temperature with OpenAI, from
`config.yml` or `LLM_TEMPERATURE`; a reasoning effort with Ollama; a level that does
not exist; an older model that takes no reasoning effort; a level the chosen model
rejects.

**Check used**: the six refused configurations were each started against the API
before the 2026-11-05 revision (T028). After the revision, the refusals for
`answer_reasoning_effort` were checked offline only.

### User Story 3 — Every model-building path behaves the same (P1; built)

These build a model: the chat and search graph, analysis summaries, the evaluator's
model under test, the evaluator's judge, the routing probe, and the retrieval
baseline's self-query. All of them go through `build_llm`, so the same settings send
the same parameters and are refused the same way. The routing probe and the answer
sweep used to ignore `config.yml`. The judge and the retrieval baseline deliberately
read no `llm:` section, so their results don't move when a deployment's config does.

**Check used**: offline, at each call site. None of the command-line tools was run on
the API.

### User Story 4 — The temperature table and its probe are gone (P2; built)

There is one rule per provider and no per-model table. Adding an OpenAI model means
naming it in `config.yml`, and nothing else.

### User Story 5 — The cost of the change is measured (P2; not kept)

The aim was a committed record of first-token and complete latency, and cost per
question, for gpt-6-luna on the sweep's questions on the served path. The tool was
written (R7), then left out of the repository on 2026-11-09 without being run. No
record exists.

## Requirements of this work, and where they stand (checked 2026-11-09)

| ID | Required by the work | Today |
|---|---|---|
| FR-001 | OpenAI calls send no temperature | Holds (`resolve_model_parameters`, `src/agent/graph.py:139`) |
| FR-002 | OpenAI calls send a reasoning effort: answer calls send `llm.answer_reasoning_effort`, otherwise `medium`; every other call sends `llm.reasoning_effort`, otherwise `low` (revised 2026-11-05; was one level, default `medium`) | Holds (`graph.py:125-129`, `build_llm(..., answer=True)`) |
| FR-002a | Answer calls are the calls whose text reaches the user as the answer: the RAG answer, every round of the live-data tool loop, the reply to a refused question, the analysis summary, and the parked Cross-Database final summary. No other call is an answer call | Holds. `AgentGraph` builds `llm` and `answer_llm` (`graph.py:434-487`). Uses: `react_to_me.py:68-85`, `plantreactome.py`, `cross_database.py:73`, the `answer_llm=` keyword on the Reactome and Plant Reactome RAG factories, `_summary_llm` (`src/api/analysis_summary.py:350`, from `AgentGraph.answer_model`) |
| FR-003 | `llm.reasoning_effort` and `llm.answer_reasoning_effort`, each optional, each one of `none`, `minimal`, `low`, `medium`, `high`, `xhigh`. Whether the model accepts the level is the API's answer (FR-013), not a list kept here | Holds (`src/util/config_yml/models.py:45-55`) |
| FR-004 | Ollama keeps its temperature behaviour and is sent no reasoning effort | Holds |
| FR-005 | Startup refuses: (a) `llm.temperature` with OpenAI, at any value, `1` included, because accepting the one value that does nothing would let an operator think they had chosen it; (b) `LLM_TEMPERATURE` with OpenAI; (c) either effort key with Ollama; (d) a level not listed in FR-003 | Holds. (a) to (c) in `resolve_model_parameters`, the same for both kinds of call; (d) when the config is parsed |
| FR-006 | One shared decision for every model-building path (Story 3). The judge and the retrieval baseline read no `llm:` section | Holds (`resolve_model_parameters`, passed to `build_llm`, `graph.py:300`). Judge: `evaluator.py:290`; baseline: `bin/retrieval_baseline:170` |
| FR-007 | The routing probe uses the deployment's `llm:` config | Holds (`src/evaluation/routing_probe.py:146`) |
| FR-008 | The safety checker, intent classifier and completeness grader return valid structured output while a reasoning effort is sent | Verified at `medium` for one question each (R1). They now run at `low`, which was not verified |
| FR-009 | The built-in default model is gpt-6-luna. A model named in `config.yml` or `LLM_MODEL` is kept | Holds (`graph.py:91`) |
| FR-010 | Startup logs the provider, the model and both sets of parameters | Holds (`graph.py:424`) |
| FR-011 | The temperature table, its logic and its probe are removed | Holds. The table's tests went with it |
| FR-012 | Comments, templates, the config schema and docs describe the new behaviour | Holds: `config_default.yml`, `.config.schema.yaml`, `env_template`, `deploy/beta/env.beta.template`, `src/evaluation/README.md` |
| FR-013 | Before other work, send one request for each distinct set of parameters. Stop if the API rejects it, quoting the provider's error with a fix. If the API gives no answer (unreachable, 429, 5xx), log a warning and start (revised 2026-11-09; was "stop on any failure"). Ollama is not checked | Holds (`check_model`, `graph.py:223`). Two requests at default settings, one if both levels are equal |
| FR-014 | The evaluator's default judge is gpt-6-astra, not the answering model. Its default model under test is the built-in default | Holds (`evaluator.py:71, 461`) |

## Out of scope (set 2026-11-03)

- A level for each task, such as a separate one for the classifier. There are exactly
two levels. Different models for different surfaces (spec 003 Story 3) are also out.
- Prompt changes, the embedding model, and an environment variable for reasoning
effort.
- Keeping gpt-4o, gpt-4o-mini, gpt-4.1 and gpt-4.1-mini working while they are
retired. A deployment that pins one of them refuses to start until it changes model.

## Open items (recorded 2026-11-09)

- **Live-data answers are unverified.** The live loop uses function tools, and gpt-5.6
refused function tools whenever a reasoning effort was sent. Nothing has tried the
combination on gpt-6-luna. If it fails, live-data questions fail for users.
- **The graders at `low`** (FR-008), and the sweep, the streaming path and the
command-line tools on the API, were not run.
- **chat-latest** accepts only `medium` (R1), so with the default `low` it refuses to
start until `llm.reasoning_effort: medium` is set.
- **The judge now runs at `low`**, because it is not an answer call. That changes its
scores. Judging at `medium` would be a deliberate choice.
- **Scores are not comparable across the judge change.** Scores judged by gpt-4o
cannot be compared with scores judged by gpt-6-astra.
- **The chat's cost counter shows $0 for these models.** `OpenAICallbackHandler.total_cost`
(`src/util/chainlit_helpers.py:95`) records $0 for any model missing from
langchain-community's price table, gpt-5.6-luna and gpt-6-luna included. That is
outside this spec.

## What was built, 2026-11-03 to 2026-11-05

- **One rule per provider, decided once.** `resolve_model_parameters` returns what to
send (`ModelParameters` in `src/agent/models.py`). `get_llm` takes it as a required
argument, so a caller that skips the decision does not type-check. The routing
probe was one such caller, silently sending `0.0`.
- **A startup request.** `check_model` sends one request with exactly the parameters
later calls will send, before the roughly 85 seconds of BM25 indexing.
A refusal is a `ModelConfigError`, which startup turns into an exit. If the API
gives no answer (an outage), startup logs a warning and goes on. `_summary_llm`
runs during a request, so it resolves nothing: it builds from
`AgentGraph.answer_model`, which startup resolved and checked, and reads no
config.yml that could fail the request.
- **Two copies of one model.** Every profile is given `(llm, answer_llm, embedding)`.
- **Deleted**: the temperature table, `resolve_temperature`, `bin/probe_model_temperature`
and their tests.
- **Also fixed** (principle VI): the routing probe and the answer sweep now read
`config.yml`, and the retrieval baseline builds its self-query model once, only when
self-query is on.
- **Added for measurement**: `astream_answer` takes `callbacks`, as `ainvoke` already
does. The measurement tool that used it was not kept.
Loading