keyring
keyring
¶
API key keyring — rotation, cooldown tracking, key selection.
The conductor maintains a keyring of API keys per instrument. Keys are NEVER stored in config files, score YAML, or anything in the git repo. Keys live in $SECRETS_DIR/ and are referenced by path.
Key files are read at selection time. The key value is returned to the caller and not cached — minimizing the time secrets live in memory.
Thread safety: all mutable state is protected by an asyncio Lock.
Classes¶
ApiKeyKeyring
¶
Manages API key selection with rotation and rate limit tracking.
Reads KeyringConfig from daemon config. Loads keys from disk at selection time (path references, never caches values). Selects keys via configurable rotation policies.
Usage::
keyring = ApiKeyKeyring(config.keyring)
if keyring.has_keys("openrouter"):
key = await keyring.select_key("openrouter")
# Use key for API request
# If rate limited:
keyring.report_rate_limit("openrouter", key_index=0, cooldown_seconds=30.0)
Source code in src/marianne/daemon/keyring.py
Functions¶
has_keys
¶
select_key
async
¶
Select and load an API key for the given instrument.
Reads the key file from disk. The key value is returned to the caller and not cached in the keyring.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instrument_name
|
str
|
Instrument to select a key for. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The API key string. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If no keys are configured for the instrument. |
FileNotFoundError
|
If the key file doesn't exist. |
ValueError
|
If the key file is empty. |
Source code in src/marianne/daemon/keyring.py
report_rate_limit
¶
Report that a key hit a rate limit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instrument_name
|
str
|
The instrument whose key was rate-limited. |
required |
key_index
|
int
|
Index of the key in the instrument's key list. |
required |
cooldown_seconds
|
float
|
How long to wait before retrying this key. |
required |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the instrument is not configured. |
IndexError
|
If key_index is out of range. |
Source code in src/marianne/daemon/keyring.py
get_key_label
¶
Get the human-readable label for a key.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instrument_name
|
str
|
The instrument name. |
required |
key_index
|
int
|
Index of the key. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The label string. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If the instrument is not configured. |
IndexError
|
If key_index is out of range. |