Logging¶
Bases: LLM
Wrapper that adds logging to any LLM instance.
Logs all requests asynchronously (fire-and-forget) without blocking the main request flow. Stores metrics in a database and optionally stores request/response bodies in S3.
Example
from majordomo_llm import get_llm_instance from majordomo_llm.logging import LoggingLLM, PostgresAdapter, S3Adapter
llm = get_llm_instance("anthropic", "claude-sonnet-4-20250514") db = await PostgresAdapter.create(host="localhost", ...) storage = await S3Adapter.create(bucket="my-bucket") logged_llm = LoggingLLM(llm, db, storage)
response = await logged_llm.get_response("Hello!")
Source code in src/majordomo_llm/logging/wrapper.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
__init__ ¶
__init__(llm, database, storage=None)
Initialize the logging wrapper.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
llm
|
LLM
|
The LLM instance to wrap. |
required |
database
|
DatabaseAdapter
|
Database adapter for storing metrics. |
required |
storage
|
StorageAdapter | None
|
Optional storage adapter for request/response bodies. |
None
|
Source code in src/majordomo_llm/logging/wrapper.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
close
async
¶
close()
Wait for pending tasks and close database and storage connections.
Source code in src/majordomo_llm/logging/wrapper.py
241 242 243 244 245 246 | |
flush
async
¶
flush()
Wait for all pending logging tasks to complete.
Source code in src/majordomo_llm/logging/wrapper.py
236 237 238 239 | |
get_json_response
async
¶
get_json_response(
user_prompt,
system_prompt=None,
temperature=0.3,
top_p=1.0,
)
Get a JSON response from the LLM with logging.
Source code in src/majordomo_llm/logging/wrapper.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
get_response
async
¶
get_response(
user_prompt,
system_prompt=None,
temperature=0.3,
top_p=1.0,
)
Get a plain text response from the LLM with logging.
Source code in src/majordomo_llm/logging/wrapper.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
get_structured_json_response
async
¶
get_structured_json_response(
response_model,
user_prompt,
system_prompt=None,
temperature=0.3,
top_p=1.0,
)
Get a structured response validated against a Pydantic model with logging.
Source code in src/majordomo_llm/logging/wrapper.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
Bases: DatabaseAdapter
PostgreSQL adapter for logging LLM requests.
Source code in src/majordomo_llm/logging/adapters/postgres.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
close
async
¶
close()
Close the connection pool.
Source code in src/majordomo_llm/logging/adapters/postgres.py
73 74 75 | |
create
async
classmethod
¶
create(
host,
port,
database,
user,
password,
min_size=1,
max_size=10,
)
Create a new PostgresAdapter with a connection pool.
Source code in src/majordomo_llm/logging/adapters/postgres.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
insert
async
¶
insert(entry)
Insert a log entry into the database.
Source code in src/majordomo_llm/logging/adapters/postgres.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
Bases: DatabaseAdapter
SQLite adapter for logging LLM requests.
Provides a lightweight, zero-setup option for local development and examples. The database file and table are created automatically.
Example
db = await SqliteAdapter.create("llm_logs.db") logged_llm = LoggingLLM(llm, db)
Source code in src/majordomo_llm/logging/adapters/sqlite.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
close
async
¶
close()
Close the database connection.
Source code in src/majordomo_llm/logging/adapters/sqlite.py
97 98 99 | |
create
async
classmethod
¶
create(database_path)
Create a new SqliteAdapter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
database_path
|
str
|
Path to the SQLite database file. Use ":memory:" for an in-memory database. |
required |
Returns:
| Type | Description |
|---|---|
SqliteAdapter
|
A configured SqliteAdapter instance. |
Source code in src/majordomo_llm/logging/adapters/sqlite.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
insert
async
¶
insert(entry)
Insert a log entry into the database.
Source code in src/majordomo_llm/logging/adapters/sqlite.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
Bases: DatabaseAdapter
MySQL adapter for logging LLM requests.
Source code in src/majordomo_llm/logging/adapters/mysql.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
close
async
¶
close()
Close the connection pool.
Source code in src/majordomo_llm/logging/adapters/mysql.py
74 75 76 77 | |
create
async
classmethod
¶
create(
host,
port,
database,
user,
password,
minsize=1,
maxsize=10,
)
Create a new MySQLAdapter with a connection pool.
Source code in src/majordomo_llm/logging/adapters/mysql.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
insert
async
¶
insert(entry)
Insert a log entry into the database.
Source code in src/majordomo_llm/logging/adapters/mysql.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
Bases: StorageAdapter
S3 adapter for storing request/response bodies.
Source code in src/majordomo_llm/logging/adapters/s3.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
close
async
¶
close()
Close the S3 client (no-op for aioboto3 context-managed clients).
Source code in src/majordomo_llm/logging/adapters/s3.py
76 77 78 | |
create
async
classmethod
¶
create(
bucket,
prefix="llm-logs",
region_name=None,
aws_access_key_id=None,
aws_secret_access_key=None,
)
Create a new S3Adapter.
Source code in src/majordomo_llm/logging/adapters/s3.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
upload
async
¶
upload(request_id, request_body, response_content)
Upload request and response bodies to S3.
Source code in src/majordomo_llm/logging/adapters/s3.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
Bases: StorageAdapter
Local file system adapter for storing request/response bodies.
Stores each request and response as separate JSON files in a directory. Useful for local development, debugging, and examples where S3 is overkill.
Files are stored as
{base_path}/{request_id}_request.json {base_path}/{request_id}_response.json
Example
storage = await FileStorageAdapter.create("./llm_logs") logged_llm = LoggingLLM(llm, db, storage)
Source code in src/majordomo_llm/logging/adapters/file.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | |
close
async
¶
close()
Close the storage adapter (no-op for file storage).
Source code in src/majordomo_llm/logging/adapters/file.py
74 75 76 | |
create
async
classmethod
¶
create(base_path)
Create a new FileStorageAdapter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_path
|
str | Path
|
Directory where log files will be stored. Created automatically if it doesn't exist. |
required |
Returns:
| Type | Description |
|---|---|
FileStorageAdapter
|
A configured FileStorageAdapter instance. |
Source code in src/majordomo_llm/logging/adapters/file.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 | |
upload
async
¶
upload(request_id, request_body, response_content)
Store request and response bodies as local JSON files.
Source code in src/majordomo_llm/logging/adapters/file.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |