Skip to content

Commit eaacf5c

Browse files
committed
[feat][default-max-tokens][derive max output tokens from model info][docs][context-length-docstring][expand default context length docstring]
1 parent b0033a0 commit eaacf5c

1 file changed

Lines changed: 29 additions & 3 deletions

File tree

swarms/structs/agent.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,8 @@ def __init__(
545545

546546
if self.context_length is None:
547547
self.context_length = self._default_context_length()
548+
549+
self.max_tokens = self._default_max_tokens() or 16000
548550

549551
if self.max_loops == "auto":
550552
self.system_prompt += (
@@ -3051,10 +3053,20 @@ def bulk_run(self, inputs: List[Dict[str, Any]]) -> List[str]:
30513053
logger.info(f"Error running bulk run: {error}", "red")
30523054

30533055
def _default_context_length(self) -> int:
3054-
"""The model's input window, or 16000 when it can't be determined.
3056+
"""
3057+
Returns the maximum input token window for the agent's underlying model.
3058+
3059+
Attempts to determine the input (context) window based on the current model name by checking
3060+
for the "max_input_tokens" property. If the value can't be determined (e.g., unknown model or
3061+
missing field), defaults to 16000.
30553062
3056-
``get_max_tokens`` is the wrong source here — it reports max *output*
3057-
tokens (32768 for gpt-4.1, against a 1047576 input window).
3063+
Returns:
3064+
int: The maximum number of input tokens for the model. Returns 16000 if undetermined.
3065+
3066+
Notes:
3067+
- Do NOT use ``get_max_tokens`` for context (input window). That reports max *output* tokens,
3068+
which may not correspond to available context for input (e.g., 32768 for gpt-4.1 output, but
3069+
over a million for certain input windows).
30583070
"""
30593071
try:
30603072
return (
@@ -3065,6 +3077,20 @@ def _default_context_length(self) -> int:
30653077
)
30663078
except Exception:
30673079
return 16000
3080+
3081+
def _default_max_tokens(self) -> int:
3082+
"""
3083+
Returns the maximum output token count for the agent's underlying model.
3084+
3085+
Determines the model's output window (number of output tokens) by checking for
3086+
the "max_output_tokens" property in the model info. Returns 16000 as default if the
3087+
property does not exist or can't be determined.
3088+
3089+
Returns:
3090+
int: The maximum number of output tokens for the model. Returns 16000 if undetermined.
3091+
"""
3092+
return get_model_info(self.model_name).get("max_output_tokens") or 16000
3093+
30683094

30693095
def reliability_check(self):
30703096

0 commit comments

Comments
 (0)