0%

Taking It Home · practice

Point the Client at a Real Provider

The key now comes from the environment, but the address and the model are still written into the file: BASE_URL = COURSE_BASE_URL and MODEL = COURSE_MODEL. To run the program anywhere else, you would have to edit it, and then edit it back to run it here. This lesson removes that last reason to touch the code.

Three settings

OpenRouter, Ollama, LM Studio and most hosted providers speak the same chat completions API this course has used since Chapter 1. The request has the same model and messages, the reply has the same choices and usage, and the openai client works with all of them. Moving the study assistant to another provider takes three settings:

  • the base URL, the address the client sends requests to;

  • the model name, which each provider spells its own way;

  • the key, which Lesson 1 already reads from the environment.

Read all three from the environment, with the course’s values as defaults, and the same file runs in the sandbox with nothing set and at home with three variables set.

Read the constants from the environment

Change the two constants at the top of study_assistant.py:

BASE_URL = os.environ.get("STUDY_ASSISTANT_BASE_URL", COURSE_BASE_URL)
MODEL = os.environ.get("STUDY_ASSISTANT_MODEL", COURSE_MODEL)

These lines run once, when the module is imported, which is when the program starts. So a variable has to be set before the program starts. Setting it in another , or after the program has started, changes nothing for that run.

Nothing else needs to change for the model to reach every request. Chapter 11’s use_settings already sets reliable.MODEL = MODEL at the start of every run, so the agent’s steps, the quiz and the chat all send this MODEL.

Report a missing key

With a provider’s address comes the chance of a missing key, and Lesson 1’s read_api_key raises ValueError for it inside build_client. Make main catch that error around the build_client() call only:

if client is None:
    try:
        client = build_client()
    except ValueError as error:
        print(f"Error: {error}", file=sys.stderr)
        return 1

That is one line on standard error that starts with Error: , like every other failure, and exit code 1, before any request is sent. Keep the try around build_client() alone. A wider try would hide a ValueError that means a bug somewhere else, and run_command already turns the command’s own errors into exit codes. A caller that passes its own client, as every test and check does, never builds one and needs no key.

Real providers

For OpenRouter, the three values are:

export STUDY_ASSISTANT_API_KEY=sk-or-...
export STUDY_ASSISTANT_BASE_URL=https://openrouter.ai/api/v1
export STUDY_ASSISTANT_MODEL=openai/gpt-oss-20b

openai/gpt-oss-20b is the model behind this course’s pl-live. It supports tools and strict JSON schemas, which ask and quiz need. In PowerShell, each line is $env: with the name, then = "..." with the value.

For Ollama, which runs models on your own computer, the address is http://localhost:11434/v1, the key can be any text such as ollama (it checks none), and the model is one you have pulled. Small local models often handle tools poorly, so ask may stop without an answer or fail its citation check while quiz and chat work.

Whichever you pick, a real model shows what the course model hid. Its answers change from run to run, as Chapter 10 showed, so ask may need its corrective retry, or fail where the scripted model never did. And its token counts are its own, not the scripted rule of characters divided by four.

Run it

Run python stages.py. The Lesson 2 block prints the settings this run uses:

Lesson 2: the active settings
  BASE_URL: http://llm-api:8080/v1
  MODEL: pl-scripted
  STUDY_ASSISTANT_API_KEY: not set
  To change them, set STUDY_ASSISTANT_BASE_URL, STUDY_ASSISTANT_MODEL and
  STUDY_ASSISTANT_API_KEY before the program starts. On this site, leave them unset.

Nothing is set, so the program uses the course model, exactly as before. stages.py still stops at Lesson 4’s work.

Now set a provider’s address for one command in the terminal, with no key:

STUDY_ASSISTANT_BASE_URL=https://openrouter.ai/api/v1 python study_assistant.py ask "How do I make an independent copy of a list?"
echo $?
Error: Set the STUDY_ASSISTANT_API_KEY environment variable to your API key for https://openrouter.ai/api/v1.
1

With a made-up key as well, the program gets as far as the request, and ends with Error: Connection error. and exit code 1 after its retries, because the sandbox has no internet. Real providers are for home, in Lesson 5.

Try it live

This step is optional. The course’s own live model is a real provider behind llm-api, so you can point the program at it with one variable and no key:

STUDY_ASSISTANT_MODEL=pl-live python study_assistant.py ask "How do I make an independent copy of a list?"

It usually uses two or three of your daily live requests, one per agent step, and at most seven if the model keeps searching or its answer needs the corrective retry. Whatever you send leaves Python Land, so send nothing private. If your account has no live access, the program exits 1 with Error: Error code: 403 - ..., the live model’s refusal on one line. A 429 means you reached a limit for now. A read timeout is a normal outcome too: a live model can think for longer than this course’s timeout.

You run export STUDY_ASSISTANT_MODEL=pl-live in one terminal, then start python study_assistant.py ask "..." in a second terminal. Which model does the run send?

Why does main keep its try around the build_client() call alone?

Task

In study_assistant.py:

  1. Change the two constants to BASE_URL = os.environ.get("STUDY_ASSISTANT_BASE_URL", COURSE_BASE_URL) and MODEL = os.environ.get("STUDY_ASSISTANT_MODEL", COURSE_MODEL).

  2. In main, when client is None, call build_client() inside try. On ValueError, print f"Error: {error}" to sys.stderr and return 1 before running the command. Keep the try around build_client() only, and leave the rest of main as it is.

Run python stages.py to see the active settings, try the missing-key error with python study_assistant.py, then check your work.