Skip to content

router catalog

The router's model list, which the Dynamiq LLM node reads its model's capabilities from.

RouterCatalog

The model lists of the routers the process calls, by router address.

A router lists the same models to every caller, so the cache holds no caller's data. A list is fetched again after MODEL_LIST_TTL_SECONDS, and a failed fetch keeps the last list and is not retried for MODEL_LIST_RETRY_SECONDS. A fetch the router refuses, such as for a revoked key, is retried later only for that caller's key, since it says nothing about the list other callers get. Safe for concurrent use: one thread fetches a router's list while the others wait for it.

Source code in dynamiq/nodes/llms/_router_catalog.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
class RouterCatalog:
    """The model lists of the routers the process calls, by router address.

    A router lists the same models to every caller, so the cache holds no caller's data. A list is fetched again after
    `MODEL_LIST_TTL_SECONDS`, and a failed fetch keeps the last list and is not retried for `MODEL_LIST_RETRY_SECONDS`.
    A fetch the router refuses, such as for a revoked key, is retried later only for that caller's key, since it says
    nothing about the list other callers get. Safe for concurrent use: one thread fetches a router's list while the
    others wait for it.
    """

    def __init__(self):
        self._lock = threading.Lock()
        self._fetch_locks: dict[str, threading.Lock] = {}
        self._routers: dict[str, _RouterModels] = {}
        self._refused: dict[tuple[str, str], float] = {}

    def lookup(self, router_url: str, slug: str) -> ModelInfo | None:
        """The model's capabilities as last fetched, or None for a model the list lacks or a router not yet fetched."""
        with self._lock:
            router = self._routers.get(router_url)
        return (router.models or {}).get(slug) if router else None

    def get(
        self, router_url: str, api_key: str, slug: str, fetch: Callable[[], Iterable[dict[str, Any]]]
    ) -> ModelInfo | None:
        """`lookup`, after fetching the router's list with `fetch`, which sends `api_key`, when it is due."""
        caller = _fingerprint(api_key)
        if self._due(router_url, caller):
            with self._fetch_lock(router_url):
                if self._due(router_url, caller):
                    try:
                        self._store(router_url, caller, list(fetch()))
                    except Exception as e:
                        self._failed(router_url, caller, e)
        return self.lookup(router_url, slug)

    async def aget(
        self, router_url: str, api_key: str, slug: str, fetch: Callable[[], Awaitable[Iterable[dict[str, Any]]]]
    ) -> ModelInfo | None:
        """`get` with an async `fetch`. No lock is held across it, so two coroutines may fetch a due list at once."""
        caller = _fingerprint(api_key)
        if self._due(router_url, caller):
            try:
                self._store(router_url, caller, list(await fetch()))
            except Exception as e:
                self._failed(router_url, caller, e)
        return self.lookup(router_url, slug)

    def clear(self) -> None:
        with self._lock:
            self._routers.clear()
            self._refused.clear()

    def _fetch_lock(self, router_url: str) -> threading.Lock:
        with self._lock:
            return self._fetch_locks.setdefault(router_url, threading.Lock())

    def _due(self, router_url: str, caller: str) -> bool:
        now = _now()
        with self._lock:
            router = self._routers.get(router_url)
            refused_at = self._refused.get((router_url, caller))
        if refused_at is not None and now - refused_at < MODEL_LIST_RETRY_SECONDS:
            return False
        if router is None:
            return True
        if router.failed_at is not None and now - router.failed_at < MODEL_LIST_RETRY_SECONDS:
            return False
        return router.fetched_at is None or now - router.fetched_at >= MODEL_LIST_TTL_SECONDS

    def _failed(self, router_url: str, caller: str, error: Exception) -> None:
        status = getattr(error, "status_code", None)
        logger.warning(
            f"Failed to list the models of the router at {_redacted(router_url)}, keeping the last list: "
            f"{type(error).__name__}" + (f" {status}" if isinstance(status, int) else "")
        )
        now = _now()
        with self._lock:
            if _refused(error):
                # Kept only while it holds a caller back, so revoked or rotated keys do not pile up.
                self._refused = {key: at for key, at in self._refused.items() if now - at < MODEL_LIST_RETRY_SECONDS}
                self._refused[(router_url, caller)] = now
            else:
                self._routers.setdefault(router_url, _RouterModels()).failed_at = now

    def _store(self, router_url: str, caller: str, models: list[Any]) -> None:
        parsed = _parsed(models)
        if parsed is None:
            raise ValueError("the model list has no entry that parses")
        with self._lock:
            router = self._routers.setdefault(router_url, _RouterModels())
            router.models = parsed
            router.fetched_at, router.failed_at = _now(), None
            self._refused.pop((router_url, caller), None)

aget(router_url, api_key, slug, fetch) async

get with an async fetch. No lock is held across it, so two coroutines may fetch a due list at once.

Source code in dynamiq/nodes/llms/_router_catalog.py
126
127
128
129
130
131
132
133
134
135
136
async def aget(
    self, router_url: str, api_key: str, slug: str, fetch: Callable[[], Awaitable[Iterable[dict[str, Any]]]]
) -> ModelInfo | None:
    """`get` with an async `fetch`. No lock is held across it, so two coroutines may fetch a due list at once."""
    caller = _fingerprint(api_key)
    if self._due(router_url, caller):
        try:
            self._store(router_url, caller, list(await fetch()))
        except Exception as e:
            self._failed(router_url, caller, e)
    return self.lookup(router_url, slug)

get(router_url, api_key, slug, fetch)

lookup, after fetching the router's list with fetch, which sends api_key, when it is due.

Source code in dynamiq/nodes/llms/_router_catalog.py
112
113
114
115
116
117
118
119
120
121
122
123
124
def get(
    self, router_url: str, api_key: str, slug: str, fetch: Callable[[], Iterable[dict[str, Any]]]
) -> ModelInfo | None:
    """`lookup`, after fetching the router's list with `fetch`, which sends `api_key`, when it is due."""
    caller = _fingerprint(api_key)
    if self._due(router_url, caller):
        with self._fetch_lock(router_url):
            if self._due(router_url, caller):
                try:
                    self._store(router_url, caller, list(fetch()))
                except Exception as e:
                    self._failed(router_url, caller, e)
    return self.lookup(router_url, slug)

lookup(router_url, slug)

The model's capabilities as last fetched, or None for a model the list lacks or a router not yet fetched.

Source code in dynamiq/nodes/llms/_router_catalog.py
106
107
108
109
110
def lookup(self, router_url: str, slug: str) -> ModelInfo | None:
    """The model's capabilities as last fetched, or None for a model the list lacks or a router not yet fetched."""
    with self._lock:
        router = self._routers.get(router_url)
    return (router.models or {}).get(slug) if router else None

model_info_from_catalog(model)

The capabilities of a model in the router's list. Raises for an entry that does not describe them.

Prompt caching is left out: the catalog says whether a model caches at all, while the node asks whether it needs cache breakpoints, which only Claude does.

Source code in dynamiq/nodes/llms/_router_catalog.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
def model_info_from_catalog(model: dict[str, Any]) -> ModelInfo:
    """The capabilities of a model in the router's list. Raises for an entry that does not describe them.

    Prompt caching is left out: the catalog says whether a model caches at all, while the node asks whether it needs
    cache breakpoints, which only Claude does.
    """
    inputs = model.get("input_modalities") or []
    if not isinstance(inputs, list):
        raise ValueError("input_modalities is not a list")
    return ModelInfo(
        max_input_tokens=model.get("context_length"),
        supports_vision="image" in inputs,
        supports_pdf_input="file" in inputs,
        supports_video_input="video" in inputs,
        supports_function_calling=model.get("supports_tools"),
        supports_response_schema=model.get("supports_structured_output"),
    )