Python API: Keys and environment

Loading API keys from a file or the environment. Generated from the docstrings and type hints of criogenai 0.1.7. See also: Python API overview, the same module in TypeScript.

API keys from the environment, and a .env file to keep several in.

Each provider reads its key from its usual variable, so you can keep all of them side by side and switch models by changing a string:

OPENAI_API_KEY      `"openai:gpt-5-mini"`
ANTHROPIC_API_KEY   `"anthropic:claude-sonnet-5-5"`
GEMINI_API_KEY      `"gemini:gemini-2.5-flash"`
OPENROUTER_API_KEY  `"openrouter:meta-llama/llama-3.3-70b-instruct"`
DEEPSEEK_API_KEY    `"deepseek:deepseek-chat"`
GROQ_API_KEY        `"groq:llama-3.3-70b-versatile"`

and the same for mistral, xai, cerebras, together, fireworks, qwen (DASHSCOPE_API_KEY), moonshot and zai (key_name(provider) gives the variable).

load_env() reads a .env file into the process environment (call it before you build a model). It never prints or returns a value, and never replaces a variable that is already set unless you ask.

from criogenai import env

env.parse_env(...)
from criogenai import load_env

These are used as env.<name>. Also exported at the top level: load_env.

Contents#

NameKindSummary
parse_envfunctionParses the text of a .env file into a dict, without touching the environment.
load_envfunctionReads a .env file into os.environ; returns the names it set.
statusfunctionWhich providers have a key right now: {"openai": True, "gemini": False, ...}.
key_namefunctionThe environment variable a provider's key comes from.

parse_env#

def parse_env(text: str) -> Dict[str, str]

Parses the text of a .env file into a dict, without touching the environment.

Accepts NAME=value lines, export NAME=value, # comments (whole lines, or after a value following a space) and values in single or double quotes. Variables are not expanded.

Parameters

  • text (str): The file's content.

Returns: {name: value} in file order; a later line wins over an earlier one with the same name.

Raises

  • ValueError: A line is malformed. The message names the line number, never its content (it may be a secret).

Example

from criogenai.env import parse_env

values = parse_env("# keys\nexport MODE=test\nGREETING='hello world'  # quoted\n")
assert values == {"MODE": "test", "GREETING": "hello world"}

load_env#

def load_env(
    path: Union[str, 'os.PathLike[str]'] = '.env',
    *,
    override: bool = False,
    required: bool = False,
) -> List[str]

Reads a .env file into os.environ; returns the names it set.

Call it before you build a model. Never returns or prints a value. Keep the file out of git (add .env to .gitignore).

Parameters

  • path (Union[str, 'os.PathLike[str]'], default '.env'): The file to read. Default: .env in the current directory.
  • override (bool, default False): Replace variables that are already set. By default they are kept.
  • required (bool, default False): Raise when the file is missing. By default a missing file is fine and nothing is set.

Returns: The names of the variables it set, sorted.

Raises

  • FileNotFoundError: The file is missing and required=True.
  • ValueError: The file is over 1 MB or has a malformed line.

Example

import criogenai

criogenai.load_env()  # reads ./.env if there is one
agent = criogenai.Agent("openai:gpt-5-mini")

status#

def status() -> Dict[str, bool]

Which providers have a key right now: {"openai": True, "gemini": False, ...}. Names and yes/no only, never a key.

key_name#

def key_name(provider: str) -> str

The environment variable a provider's key comes from.

Parameters

  • provider (str): A provider name, such as "openai" or "qwen".

Returns: The variable's name, such as "OPENAI_API_KEY" or "DASHSCOPE_API_KEY".

Raises

  • ValueError: The provider has no single key variable: unknown, or one such as ollama, kaggle or azure.

Example

from criogenai.env import key_name

assert key_name("openai") == "OPENAI_API_KEY"