fix(llm): add conditional max_tokens parameter naming per provider#340
Merged
Conversation
LLM API providers use different parameter names for max output tokens:
- OpenAI (legacy): max_tokens
- OpenAI (new): max_completion_tokens
- Anthropic: max_tokens
- Google Gemini/Vertex: max_output_tokens
cora-cli previously hardcoded 'max_tokens' everywhere, causing HTTP 400 errors
('Unsupported parameter: max_output_tokens') when providers don't accept it.
Changes:
- Add max_tokens_param to LlmSection config schema (supports 'auto' for
provider-based detection, or explicit override)
- Add resolve_max_tokens_param() in loader.rs for auto-detection logic
- Replace hardcoded 'max_tokens' JSON key with dynamic key in both
streaming and non-streaming LLM request paths
- Add max_tokens_param to LLMConfig struct for runtime use
- Display max_tokens_param in 'cora config' output
Auto-detection maps:
- gemini/google/vertex -> max_output_tokens
- All other providers -> max_tokens
Co-authored-by: Hermes Agent <hermes@nousresearch.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
LLM API providers use different parameter names for max output tokens:
max_tokensmax_completion_tokensmax_tokensmax_output_tokensmax_tokens→max_output_tokensand then rejectcora-cli previously hardcoded
max_tokenseverywhere. When a provider doesn't support it, we get HTTP 400:Unsupported parameter: max_output_tokens.Solution
Add
max_tokens_paramto thellmsection in.cora.yaml. Supports:auto(default): detect from provider name.gemini/google/vertex→max_output_tokensmax_tokensmax_tokens,max_output_tokens,max_completion_tokens: explicit overrideConfig example
Changes
src/config/schema.rs: Addedmax_tokens_param: Option<String>toLlmSectionandmax_tokens_param: StringtoConfig(default:"auto")src/engine/types.rs: Addedmax_tokens_param: StringtoLLMConfig(default:"max_tokens")src/config/loader.rs: Addedresolve_max_tokens_param()for auto-detection; resolved value passed toLLMConfigsrc/engine/llm.rs: Replaced hardcoded"max_tokens"JSON key with dynamicconfig.max_tokens_paramin both streaming and non-streaming request pathssrc/commands/config_cmd.rs: Displaymax_tokens_paramincora configoutputTests added
test_resolve_max_tokens_param_auto_*— auto-detection for known providers (gemini, google, vertex, openai, anthropic)test_resolve_max_tokens_param_explicit_override— explicit value wins over autotest_chat_request_uses_max_output_tokens— verify JSON key name is correcttest_chat_request_uses_max_tokens_default— verify default key namemax_tokens_parampropagationLlmSectionwithmax_tokens_paramAll 621 tests pass.
cargo fmt --all -- --checkclean.