Skip to content

fix: respect GenerationConfig on cloud LLM path#27

Merged
soyasis merged 1 commit into
cai-layer:masterfrom
akl773:fix/cloud-llm-truncation
May 20, 2026
Merged

fix: respect GenerationConfig on cloud LLM path#27
soyasis merged 1 commit into
cai-layer:masterfrom
akl773:fix/cloud-llm-truncation

Conversation

@akl773

@akl773 akl773 commented May 20, 2026

Copy link
Copy Markdown
Contributor

Fixes #26.

Summary

The cloud OpenAI-compatible branch in LLMService.generateWithMessages hardcoded temperature: 0.3, max_tokens: 1024 and ignored the config: GenerationConfig parameter that the caller passes in. The MLX branch and the Anthropic branch (line 685) honour config.maxTokens correctly — this was the only orphan path, so OpenRouter, LM Studio, Ollama and custom-URL providers always got the same hardcoded cap regardless of action.

Effect for users: long rewrites (custom shortcuts like "AI Polish", built-in Proofread on multi-paragraph input, Ask AI) were truncated mid sentence. Worst on reasoning / thinking models (Gemini 2.5 Flash, OpenAI o-series, DeepSeek R1, GPT-5-thinking, Anthropic extended thinking), where hidden reasoning tokens share the same max_tokens budget as visible output and can eat the entire 1024 before the model writes anything visible.

Changes

  1. Forward config.temperature and config.maxTokens into the ChatRequest instead of hardcoding.
  2. Raise the per-action token cap for .custom (1024 → 16384) and .proofread (800 → 16384) so rewrite-style actions have room on thinking models. Cloud providers bill on actual output tokens, so a high ceiling has no cost unless the model fills it.
  3. Bump the request timeout from 30s → 60s to match the Anthropic branch — slower OpenRouter-routed models can exceed 30s on long outputs.

The streaming wrapper generateStreamingWithMessages falls back through generateWithMessages for external providers, so it picks up the same fix without further change.

Out of scope (left for follow-up)

  • Omitting max_tokens entirely on cloud providers and letting the provider default kick in (cleaner but loses the defensive ceiling).
  • Per-shortcut "output length" override in the UI.
  • Dedicated reasoning-effort plumbing for OpenRouter's reasoning param.

Discussed in the issue thread; happy to follow up if maintainers want either of these in the same PR.

Test plan

  • Diff reviewed — only LLMService.swift touched, no behaviour change to MLX / Anthropic / Apple paths.
  • Manual: OpenRouter + Gemini 2.5 Flash, run "AI Polish" on a ~1500-word passage; confirm output is not truncated.
  • Manual: OpenRouter + GPT-4o or Claude via OR, run "Summarize" on long content; confirm timeout no longer fires at 30s for slow first-token responses.
  • Manual: LM Studio / Ollama still work with their respective local models.
  • Existing LLMService tests still pass (xcodebuild -scheme Cai -configuration Debug test).

The cloud OpenAI-compatible branch in `generateWithMessages` hardcoded
`temperature: 0.3, max_tokens: 1024` and ignored the `config` parameter,
unlike the MLX and Anthropic branches which honour it. Cloud responses
(OpenRouter, LM Studio, Ollama, custom) were therefore truncated mid
sentence on long rewrites and on any thinking-enabled model whose hidden
reasoning tokens share the same budget as visible output.

Forward `config.temperature` and `config.maxTokens` to the request, raise
the per-action ceiling for `.custom` and `.proofread` from 1024/800 to
16384 to give rewrite-style actions room on thinking models (Gemini 2.5
Flash, o-series, DeepSeek R1, etc.), and bump the request timeout from
30s to 60s to match the Anthropic branch since slower OpenRouter-routed
models can exceed 30s on long outputs.

Refs cai-layer#26.

@soyasis soyasis left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. Tested locally on OpenRouter + Gemini 2.5 Flash with a 1010-word input. Proofread and a custom shortcut both completed without truncation, temperature and maxTokens now visibly reaching ChatRequest.

@soyasis soyasis merged commit facc88a into cai-layer:master May 20, 2026
1 check passed
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.

Cloud provider (OpenRouter/LM Studio/Ollama) responses truncated — hardcoded max_tokens=1024, config ignored

2 participants