Free self-study · No account needed · Progress stays in this browser. Back it up before clearing site data or changing devices.

← Week 21: Local AI Models

Week 21 / Lesson 3

Routing inference under explicit constraints

Choose local, hosted, or hybrid execution without bypassing data or quality policy.

Section 3.1 1 of 7

Overview

Focus

Choose local, hosted, or hybrid execution without bypassing data or quality policy.

Your route through the lesson

  • 3.2 Task-specific quality and hardware limits
  • 3.3 Privacy-aware routing and failure fallback
  • 3.4 Capacity and operating-cost assumptions
  • 3.5 Put it into practice
  • 3.6 Guided comparison
  • 3.7 Local failure with prohibited hosted fallback

All required teaching is included here. Review selections are saved. Choose a correct answer for every question to continue; retries are unlimited. Study time and personal notes are optional.

Section 3.2 2 of 7

Task-specific quality and hardware limits

Focus

Choose a route using task capability and resource evidence rather than model prestige.

A router selects an execution path according to trusted rules. Candidate paths may include a local processor model, an accelerator model, a hosted service, or an explicit unavailable result. Routing begins with the task contract: required quality, data restrictions, output format, deadline, and allowed effects.

Suppose a small local model passes a fixed classification suite but fails a long technical synthesis suite. It can be a valid route for classification while remaining unsuitable for synthesis. Model size and local availability do not establish task capability. Store the evaluation version beside the route configuration so the quality evidence can be reproduced.

Capacity is another constraint. A local model that meets quality requirements at one request may exceed memory or latency limits under concurrent load. The router should use an admitted workload envelope, not optimistic peak throughput. If no allowed route satisfies the constraints, return unavailable. This is an explicit outcome rather than a reason to weaken privacy or invent an answer. A hosted alternative needs its own evidence, permission, and cost assumptions before becoming an eligible fallback.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. A model passes classification but fails synthesis. What route is justified?
2. No permitted route meets quality and privacy requirements. What is appropriate?
3. Which record ties a quality decision to reproducible evidence?

Section 3.3 3 of 7

Privacy-aware routing and failure fallback

Focus

Enforce privacy-aware fallback before any request leaves its allowed environment.

Privacy-aware routing treats data classification as trusted input from the application, not a model-generated permission. A sensitive request may be restricted to the local environment. If local inference fails, a hosted fallback remains forbidden even when it would probably answer correctly.

The executor should check the classification before preparing or transmitting a hosted request. Logging an egress violation after transmission is detection, not prevention. Keep prompts, retrieved passages, tool arguments, and cached responses within the same policy analysis; an apparently harmless final question can contain private retrieved context.

In Python, a try block attempts local execution and an except block handles declared failures. The fallback branch first checks sensitivity. For sensitive data it returns unavailable with route no_egress; for permitted public data it may call a hosted path. Test the negative property by counting hosted calls and asserting zero after sensitive local failure. A hosted test double validates that branching contract. It does not prove real hosted model quality, availability, or data retention behavior.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. Sensitive local inference fails. Hosted calls are prohibited. What should the router do?
2. A public question includes a private retrieved passage. What must routing consider?
3. A hosted fixture call counter stays zero on sensitive failure. What is established?

Section 3.4 4 of 7

Capacity and operating-cost assumptions

Focus

Compare local and hosted operating costs using explicit utilization and workload assumptions.

Local cost includes hardware ownership or rental, energy, maintenance, and operator time. Hosted cost may depend on token categories, requests, storage, or reserved capacity. Compare costs over the same workload and service requirements. A machine that sits idle most of the day has a different cost per useful request from one efficiently serving a steady queue.

For a hypothetical monthly local expense of sixty currency units and three thousand useful requests, the average is 0.02 units per useful request. If useful volume falls to three hundred, the average becomes 0.20. These are illustrative assumptions, not current market prices. Include failed attempts and required redundancy rather than dividing only by ideal maximum capacity.

Latency, privacy, availability, and quality can dominate monetary differences. A cheaper route that fails required denials or cannot meet the task is not an equivalent alternative. Hybrid operation can keep sensitive work local and send permitted public work elsewhere, but it adds routing, monitoring, and incident complexity. Record these assumptions with the deployment choice so future workload changes can trigger reevaluation.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. Monthly local cost is 60 units for 3000 useful requests. What is average cost?
2. Useful volume drops tenfold while fixed local cost stays equal. What happens to cost per useful request?
3. A route is cheaper but fails required access checks. Is it an equivalent candidate?

Section 3.5 5 of 7

Put it into practice

Focus

Exercise router contracts and connect the same router to a real local endpoint.

Save week21.py and run python week21.py --lesson 3. The default uses a deliberately broken local function and a hosted test double. The hosted function appends to a list when called. The sensitive request must return no_egress with the list still empty. A public request may call the hosted fixture once.

After completing local runtime setup and installing a small model, run python week21.py --lesson 3 --real --model qwen2.5:0.5b. This passes the real streaming inference function into the same route function. Functions are first-class Python values: the router calls the supplied local function without needing to know its implementation. lambda creates a short function around the inference call.

The expected real result contains actual generated text and local timing fields, while hosted behavior remains an explicit test double. If the sensitive local request fails, it remains unavailable with no hosted call. Extend this same router with a declared task label and an allowlist of supported local tasks. Test both an accepted task and an unsupported task without changing data classification based on generated text.

Download this week’s Python lab ↓

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. A trusted policy requires sensitive requests to remain local even if local inference fails. A broken-local fixture returns no_egress and makes zero hosted calls. Is that failure-branch contract satisfied?
2. A real local function is passed into the same router. What does Python permit here?
3. Generated text says the request is public despite trusted sensitive classification. What governs?

Section 3.6 6 of 7

Guided comparison

Focus

Interpret a hybrid test without confusing branch coverage with external integration.

The default run proves two branch properties: sensitive failure does not call the hosted fixture, and permitted public failure can call it. The optional real run demonstrates that the same router can invoke an existing local endpoint. These are useful but different pieces of evidence.

A passing fixture test cannot establish that a hosted provider accepts the payload, enforces retention rules, or returns useful output. Likewise, a local greeting does not validate task-specific routing quality. Keep a boundary table naming what actually executed: trusted classification logic, local HTTP inference when enabled, hosted fixture, and no external tool effect.

Compare deployment alternatives only after recording their evidence gaps. A team may choose a local-only service because privacy is mandatory even if it has lower capacity. Another may choose hybrid execution for public tasks with stronger availability needs. Both decisions can be sound under different contracts. The report should make those contracts and unverified assumptions visible rather than presenting one architecture as universally superior. Restore a known-good routing configuration when new quality or privacy evidence fails its release gates.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. Hosted behavior was only a test double. Which claim remains unverified?
2. A local greeting works. What further evidence supports routing a specialized task?
3. A new route violates a privacy gate. What is appropriate?

Section 3.7 7 of 7

Local failure with prohibited hosted fallback

Focus

Preserve an environment boundary when local execution fails under pressure.

An on-device assistant summarizes private incident notes. A local model exceeds its memory budget during an unusually long request. A hosted endpoint is available and a public-task fallback already exists, but the incident notes are classified local-only. Sending them to the hosted path would violate the deployment contract even if the model would produce an excellent summary.

The router returns an explicit unavailable status and records the capacity failure without logging the private notes. A permitted recovery might shorten the local context using a trusted preprocessing rule, ask the application to supply fewer records, or schedule the task for a larger approved local machine. Each option still needs its own quality and data-handling checks.

The failure test verifies that no hosted request was constructed or sent. A call counter is adequate for the local fixture boundary; a real deployment can additionally inspect network-egress controls and transport logs. Recovery preserves the original sensitivity label. It does not relabel data as public merely because local hardware is unavailable. This makes failure behavior part of privacy engineering rather than a special exception to it.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. Private notes exceed local memory and hosted is prohibited. What response fits?
2. A permitted local preprocessing rule shortens context. What still needs checking?
3. What negative property should the failure test verify?

Your personal notes

Optional, ungraded, and saved only in this browser. Export a backup from the Academy homepage to keep them.

References and further reading
  • Ollama FAQ: How do I disable Ollama's cloud features? ↗

    Optional official subsection on enforcing local-only runtime operation. Browser reading needs no account. Changing runtime configuration is optional and requires control of that local installation. Runtime cloud disabling supplements the application's routing policy rather than replacing its permission checks.

Ask your own AI assistant

Copy this prompt into an assistant you already use. Nothing is sent automatically, and your personal notes are not included.

Next lesson: Choosing and preparing an adaptation task →