Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions cyclops/core/compaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from cyclops.core.completion import _acompletion, _completion_kwargs, _usage
from cyclops.core.events import Compacted
from cyclops.core.providers import _asks_server, _model_info
from cyclops.core.types import AgentConfig, Compaction, Usage
from cyclops.toolkit.types import ToolResult

Expand All @@ -27,18 +28,20 @@ def _window(config: AgentConfig) -> Optional[_Limits]:
"""
windows = []
if config.router is None:
try:
info: Any = litellm.get_model_info(config.model)
except Exception: # not mapped
return None
windows.append((config.model, info))
windows.append((config.model, _model_info(config.model)))
else:
for deployment in config.router.get_model_list(config.model) or []:
try:
info = config.router.get_router_model_info(deployment, config.model)
except Exception: # not mapped
return None
windows.append((deployment["litellm_params"]["model"], info))
model = deployment["litellm_params"]["model"]
provider = deployment["litellm_params"].get("custom_llm_provider")
if _asks_server(model, provider):
# The deployment's model_info over the map's, as the router merges them.
info = {**_model_info(model, provider), **deployment["model_info"]}
else:
try:
info = config.router.get_router_model_info(deployment, config.model)
except Exception: # not mapped
return None
windows.append((model, info))
limits = [
(model, info.get("max_input_tokens"), info.get("max_output_tokens"))
for model, info in windows
Expand Down
3 changes: 2 additions & 1 deletion cyclops/core/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
_caches_at_markers,
_every_deployment,
_native_schema,
_response_schema,
_takes_param,
)
from cyclops.core.types import AgentConfig, Usage
Expand Down Expand Up @@ -57,7 +58,7 @@ def _completion_kwargs(
# With tools only where LiteLLM sends the schema natively: elsewhere it
# may emulate it with a forced tool call, which blocks the agent's tools.
if response_model is not None and (
(mode == "none" and _every_deployment(config, litellm.supports_response_schema))
(mode == "none" and _every_deployment(config, _response_schema))
or (mode == "native" and _every_deployment(config, _native_schema))
):
kwargs["response_format"] = response_model
Expand Down
56 changes: 44 additions & 12 deletions cyclops/core/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
# only for Anthropic models). Gemini turns them into billed cachedContents
# resources instead, and OpenAI caches without them.
_CACHE_MARKER_PROVIDERS = {"anthropic", "bedrock"}
# LiteLLM's model info lookups ask these providers' servers (Ollama only for
# models its map lacks), blocking the caller and ignoring the deployment's api_base.
_SERVER_INFO_PROVIDERS = {"huggingface", "lemonade", "ollama", "ollama_chat"}

# A check of one deployment: its model, its custom_llm_provider, then any arguments.
_Check = Callable[..., bool]
Expand All @@ -46,19 +49,53 @@ def _caches_at_markers(model: str, custom_llm_provider: Optional[str]) -> bool:
)


def _asks_server(model: str, custom_llm_provider: Optional[str]) -> bool:
"""True if LiteLLM's model info lookups would ask the provider's server."""
try:
provider = litellm.get_llm_provider(model, custom_llm_provider)[1]
except Exception: # unknown provider
return False
return provider in _SERVER_INFO_PROVIDERS


def _model_info(model: str, custom_llm_provider: Optional[str] = None) -> Any:
"""LiteLLM's info for the model; empty if it has none.

Where LiteLLM would ask the provider's server, its model map is read
instead: the entry under provider/model, else model, if it is for that
provider or none, as LiteLLM matches them.
"""
if _asks_server(model, custom_llm_provider):
name, provider = litellm.get_llm_provider(model, custom_llm_provider)[:2]
for key in (f"{provider}/{name}", name):
info = litellm.model_cost.get(key)
if info is not None and info.get("litellm_provider") in (None, provider):
return info
return {}
try:
return litellm.get_model_info(model, custom_llm_provider)
except Exception: # not mapped
return {}


def _native_schema(model: str, custom_llm_provider: Optional[str] = None) -> bool:
"""True if LiteLLM's own entry for this provider lists native structured output.

Not `supports_native_structured_output`: it falls back to another provider's
entry, while the Vertex AI builder reads only its own.
"""
try:
info = litellm.get_model_info(model, custom_llm_provider)
except Exception: # not mapped
return False
info = _model_info(model, custom_llm_provider)
return info.get("supports_native_structured_output") is True


def _response_schema(model: str, custom_llm_provider: Optional[str]) -> bool:
"""`litellm.supports_response_schema`, without asking the provider's server."""
if _asks_server(model, custom_llm_provider):
info = _model_info(model, custom_llm_provider)
return info.get("supports_response_schema") is True
return litellm.supports_response_schema(model, custom_llm_provider)


def _takes_param(model: str, custom_llm_provider: Optional[str], param: str) -> bool:
"""True if LiteLLM lists `param` as supported."""
supported = litellm.get_supported_openai_params(
Expand All @@ -76,16 +113,11 @@ def _lacks_tools(model: str, custom_llm_provider: Optional[str]) -> bool:


def _takes_input(model: str, custom_llm_provider: Optional[str], flag: str) -> bool:
"""False only if LiteLLM's model map sets `flag` to false for the model.
"""False only if LiteLLM's info sets `flag` to false for the model.

Most entries leave the flags out. The entry is read from litellm.model_cost
under provider/model, else model: get_model_info asks some providers
(Ollama) over the network.
Most entries leave the flags out.
"""
model, provider = litellm.get_llm_provider(model, custom_llm_provider)[:2]
costs = litellm.model_cost
info = costs.get(f"{provider}/{model}") or costs.get(model) or {}
return info.get(flag) is not False
return _model_info(model, custom_llm_provider).get(flag) is not False


def _deployments(config: AgentConfig) -> List[Dict[str, Any]]:
Expand Down
19 changes: 19 additions & 0 deletions docs/guide/agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,25 @@ The run reads each completion's `finish_reason`, as LiteLLM normalizes it across

A long conversation, or a large tool result, eventually outgrows the model's context window. Two settings keep requests within it: compaction summarizes older messages, and `max_tool_output_chars` caps each tool result. A request that still overflows is retried with what fits. Both are off by default, as in other agent libraries, since compaction makes an extra completion and replaces what the model saw with a summary.

Compaction and overflow recovery take the model's limits from LiteLLM. For Ollama, Lemonade and Hugging Face models Cyclops reads only LiteLLM's model map, since LiteLLM's `get_model_info` would ask the provider's server and block the run. A model the map lacks, such as `ollama/qwen3:4b`, gets no limits: compaction raises `ValueError` and an overflow is not retried. Register its limits at startup, keyed by the exact model string, with the context length your server runs it with:

```python
import litellm

litellm.register_model(
{
"ollama/qwen3:4b": {
"max_input_tokens": 32768,
"max_output_tokens": 8192,
"litellm_provider": "ollama",
"mode": "chat",
}
}
)
```

Registering it also stops LiteLLM's own `/api/show` requests: for an Ollama model its map lacks, LiteLLM prices each completion by asking the server, four or five requests per completion, sent mostly to `OLLAMA_API_BASE` or localhost even when the model has its own `api_base`.

### Compaction

```python
Expand Down
4 changes: 2 additions & 2 deletions docs/guide/multimodal.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ As LiteLLM 1.105 sends them: Anthropic gets `image` and `document` blocks, with

## Models that can't take the input

Before the run starts, Cyclops reads LiteLLM's model map (`litellm.model_cost`, the entry under `provider/model`, else `model`) for the model, each of its deployments in a `router` (not the router's fallback groups) and each of its `fallback_models`, since they all read the same history. If the entry sets `supports_vision` to false and an image is sent, `supports_pdf_input` to false and a PDF is sent, or `supports_audio_input` to false and audio is sent, the run raises `ValueError`, so nothing is sent and no part is dropped:
Before the run starts, Cyclops reads LiteLLM's model info (`litellm.get_model_info`, or for Ollama, Lemonade and Hugging Face only its model map, so no provider is asked over the network) for the model, each of its deployments in a `router` (not the router's fallback groups) and each of its `fallback_models`, since they all read the same history. If the entry sets `supports_vision` to false and an image is sent, `supports_pdf_input` to false and a PDF is sent, or `supports_audio_input` to false and audio is sent, the run raises `ValueError`, so nothing is sent and no part is dropped:

```text
o3-mini, one of its deployments or one of its fallback models can't take image input: LiteLLM's model map sets supports_vision to false for one of them; if that is wrong, correct its entry with litellm.register_model at startup
```

Only an explicit false counts. Most entries leave these flags out, and a model with no entry at all, such as one on a custom OpenAI compatible endpoint, gets the parts and its provider decides, as in Pydantic AI, the OpenAI Agents SDK and the Vercel AI SDK, which don't check. The check reads only `litellm.model_cost`, the map LiteLLM ships plus any entries registered in this process, never a provider over the network. Capability checks are cached per process, so call `litellm.register_model` at startup, before the first run. A file whose media type is unknown (a `file_id` without `format`) is not checked.
Only an explicit false counts. Most entries leave these flags out, and a model with no entry at all, such as one on a custom OpenAI compatible endpoint, gets the parts and its provider decides, as in Pydantic AI, the OpenAI Agents SDK and the Vercel AI SDK, which don't check. The check reads only the map LiteLLM ships plus any entries registered in this process, never a provider over the network. Capability checks are cached per process, so call `litellm.register_model` at startup, before the first run. A file whose media type is unknown (a `file_id` without `format`) is not checked.

## Steering, sessions and guards

Expand Down
Loading
Loading