Skip to content

feat(localai-client): SetMaxTokens — per-completion generation cap - #68

Open
walcz-de wants to merge 1 commit into
mudler:mainfrom
walcz-de:feat/localai-max-tokens
Open

feat(localai-client): SetMaxTokens — per-completion generation cap#68
walcz-de wants to merge 1 commit into
mudler:mainfrom
walcz-de:feat/localai-max-tokens

Conversation

@walcz-de

@walcz-de walcz-de commented Aug 2, 2026

Copy link
Copy Markdown

What

Adds a SetMaxTokens(n int) setter on LocalAIClient (parity with the existing
SetTemperature / SetReasoningEffort) and injects request.MaxTokens on both
CreateChatCompletion and CreateChatCompletionStream when n > 0. Zero leaves the
field unset (backend default applies).

Why

cogito never sets max_tokens on any request. In an agent loop with no server-side
cap, a degenerate completion (narration/emoji loop) can run to full context — neither
loop-detection nor iteration limits bound a single completion; only a per-request
max_tokens (llama.cpp n_predict) does. This gives callers a per-completion backstop.

Test

TestLocalAIClientSetMaxTokens asserts max_tokens is sent when set and omitted when unset.

cogito never set max_tokens on any request, so an agent-loop completion with no
server-side cap could run to full context (runaway narration/emoji loops that
neither loop-detection nor iteration limits can stop). Add a maxTokens field +
SetMaxTokens setter (parity with SetTemperature/SetReasoningEffort) and inject
request.MaxTokens on both CreateChatCompletion and CreateChatCompletionStream
when > 0. Zero leaves it unset (backend/YAML default applies).

Gate test: TestLocalAIClientSetMaxTokens asserts max_tokens is sent when set and
omitted when unset.

Signed-off-by: stefanwalcz <stefan.walcz@walcz.de>
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.

1 participant