AI Agents Book

Primitive of the day · 2026-11-07

lazy-singleton-model-load

Load a heavyweight model once via a module-level lazy singleton so repeated pipeline calls pay the multi-second load/download only on first use.

When this applies

A function invoked repeatedly in one process needs an expensive-to-load model.

Preconditions

Do this

Guard construction with a module-level None-check: construct on first call, reuse thereafter; document that the first call pays the download/load cost.

What you should see

First call pays the download/load; subsequent calls load from the cached singleton in under a second.

How it fails if ignored

Reloading the model on every call; the singleton ignoring a changed model_name argument after first load, silently serving the stale model.

Do not use when

Multi-process workers where module globals are not shared; models requiring different per-request configurations.

Kind: discipline. Part of the skill Build RAG reranker pipeline. Free to reuse in your own agent skills.

Get the whole skill

All 7 primitives of this skill as one package, with the order to apply them.

Get just this primitive

This one primitive for 1 credit (≈ €0.10); the other primitives of the skill are on its page.

Upgrade your own skill

Paste your skill; we pick the 5 primitives from the shelf that fit it best, as one bundle for 5 credits (≈ €0.50).

Upgrade my skill

Register interest

Interest only — no payment, no order, no reservation. After you confirm by email we send you one free sample primitive.