Skip to content

fix(llm): honor max_tokens_field in the connection test - #573

Merged
BaaRai merged 15 commits into
BaaRai:mainfrom
gabrimarch3:fix/llm-test-max-completion-tokens
Nov 26, 2026
Merged

BaaRai merged 15 commits into
BaaRai:mainfrom
gabrimarch3:fix/llm-test-max-completion-tokens

Conversation

@gabrimarch3

Copy link
Copy Markdown
Contributor

Problem

On System → LLM, a profile with format OpenAI and max tokens field set to max_completion_tokens fails the Test button against OpenAI reasoning models (o-series / GPT-5):

openai: status 400: "Unsupported parameter: 'max_tokens' is not supported with this model. Use 'max_completion_tokens' instead." (unsupported_parameter)

Conversations and tasks with the same profile work: the runtime path copies MaxTokensField from the profile into agent.Config, but POST /api/llm/test never read it. TestConnection always sets MaxTokens: 8192, so the test request carried the cap under the default max_tokens key.

Change

  • server/server.go (testLLM): accept an optional max_tokens_field. When the request omits it (the list-view test sends only profile_id), use the stored profile's value. Normalize it the same way profile saves do: only format openai with max_completion_tokens keeps it; anything else becomes "" (max_tokens).
  • web/src/lib/api.ts, web/src/app/(main)/system/llm/page.tsx: the form sends its current selection, so an unsaved change is tested as shown.

Other formats are unaffected: anthropic and openai-responses hard-code their field names, and the value is cleared for them.

Tests

  • New server/llm_test_max_tokens_field_test.go, against a fake OpenAI endpoint. It asserts which key the request body carries in four cases: form value, stored-profile fallback, explicit empty form value overriding the stored choice, and an unknown value falling back to max_tokens. It fails on main: the body carries max_tokens instead of max_completion_tokens.
  • go vet ./server/ is clean. The existing TestLLMTest* tests pass.
  • go test ./server/ with Postgres passes, except the six *AsRoot isolation tests. Those fail identically on unmodified main in my environment (run as root in a golang container on macOS/OrbStack), so they are unrelated to this change.
  • Frontend: tsc --noEmit is clean. biome lint on the touched files reports only warnings that already exist on main.

Not run: a live test against the OpenAI API.

🤖 Generated with Claude Code

Coff0xc and others added 7 commits November 26, 2026 11:40
POST /api/assets decoded with json.NewDecoder directly. A multipart
Content-Type skips the JSON body middleware, so that request was uncapped.
Decode through the shared limit first and answer 413 before touching the
database.
The Test button on System -> LLM always sent the output cap as max_tokens,
because POST /api/llm/test never read the profile's max_tokens_field. OpenAI
reasoning models (o-series, GPT-5) reject max_tokens with
unsupported_parameter, so a profile set to max_completion_tokens failed the
test with HTTP 400 while conversations with the same profile worked.

testLLM now accepts max_tokens_field from the form and falls back to the
stored profile's value when the field is omitted (list-view test). It is
normalized like profile saves: only the openai format with
max_completion_tokens keeps it. The form passes its current selection.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Username/password was offered to any client and accepted any credentials,
so a non-loopback client bypassed the loopback-only rule applied to no-auth
by offering method 0x02. Credentials are never verified, so both methods are
now accepted from loopback clients only; any other client gets 0xFF.
renderTemplate replaced placeholders param by param, so a value containing
another placeholder was expanded again inside the first value's shell
quotes: a="{b}", b="; id" rendered {a} as ''; id'' and ran id. Whether it
happened depended on map iteration order. Scan the template once and never
rescan substituted values.
limitRequestBody skipped every request with a multipart Content-Type, so
any handler decoding JSON straight from r.Body (about 30 of them) read an
unbounded body when the client simply labelled the request multipart.
Multipart bodies now get an upload-sized cap (the largest limit an upload
handler sets); upload handlers keep their own tighter limits and messages.
…example

The host filter description showed a real routable IP (107.172.96.177) to
the model as its example. Use 192.0.2.10 from TEST-NET-1 (RFC 5737) so the
example cannot prime the agent toward a third-party host.
@5P34R

5P34R commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Review notes

Looks good to merge. max_tokens_field resolves from the form, then from the stored profile, and is normalised the same way profile saves are (only openai + max_completion_tokens survives). The list-view test button omits the field, so the stored value is used. It complements #538 and doesn't overlap with it.

Small request: the other fix PRs add a CHANGELOG entry; this one doesn't yet.

@gabrimarch3

Copy link
Copy Markdown
Contributor Author

Added a CHANGELOG entry under [Unreleased] → Fixed in 823fb27. Thanks for the review.

@BaaRai
BaaRai force-pushed the fix/llm-test-max-completion-tokens branch from 10ee740 to d64c8fe Compare November 26, 2026 22:24
BaaRai added a commit to Coff0xc/ARTEX-contrib that referenced this pull request Nov 26, 2026
@BaaRai
BaaRai force-pushed the fix/llm-test-max-completion-tokens branch from d64c8fe to c63262f Compare November 26, 2026 22:40
BaaRai added a commit to Coff0xc/ARTEX-contrib that referenced this pull request Nov 26, 2026
fix: custom-tool injection, SOCKS5 open proxy, multipart body cap, example IP
fix(server): cap insert_assets request bodies
BaaRai added a commit to Coff0xc/ARTEX-contrib that referenced this pull request Nov 26, 2026
@BaaRai
BaaRai merged commit 1aaeed9 into BaaRai:main Nov 26, 2026
6 checks passed
@BaaRai

BaaRai commented Nov 26, 2026

Copy link
Copy Markdown
Owner

Merged, thanks @gabrimarch3. Rebased on current main and verified with its tests and negative control.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants