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 majordomo_llm/logging/wrapper.py
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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
__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 majordomo_llm/logging/wrapper.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 | |
close
async
¶
close()
Wait for pending tasks and close database and storage connections.
Source code in majordomo_llm/logging/wrapper.py
347 348 349 350 351 352 | |
flush
async
¶
flush()
Wait for all pending logging tasks to complete.
Source code in majordomo_llm/logging/wrapper.py
342 343 344 345 | |
get_json_response
async
¶
get_json_response(user_prompt, system_prompt=None, temperature=0.3, top_p=1.0, extra_headers=None, *, caller_metadata=None)
Get a JSON response from the LLM with logging.
Source code in majordomo_llm/logging/wrapper.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |
get_response
async
¶
get_response(user_prompt, system_prompt=None, temperature=0.3, top_p=1.0, extra_headers=None, *, caller_metadata=None)
Get a plain text response from the LLM with logging.
Source code in majordomo_llm/logging/wrapper.py
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 | |
get_response_stream
async
¶
get_response_stream(user_prompt, system_prompt=None, temperature=0.3, top_p=1.0, extra_headers=None, *, caller_metadata=None)
Get a streaming text response from the LLM with logging.
Source code in majordomo_llm/logging/wrapper.py
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 247 248 249 250 251 252 253 254 | |
get_structured_json_response
async
¶
get_structured_json_response(response_model, user_prompt, system_prompt=None, temperature=0.3, top_p=1.0, extra_headers=None, *, caller_metadata=None)
Get a structured response validated against a Pydantic model with logging.
Source code in majordomo_llm/logging/wrapper.py
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | |
Bases: DatabaseAdapter
PostgreSQL adapter for logging LLM requests.
Source code in 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 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 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 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 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 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 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 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 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 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 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 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 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | |
close
async
¶
close()
Close the S3 client (no-op for context-managed clients).
Source code in majordomo_llm/logging/adapters/s3.py
96 97 98 | |
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 majordomo_llm/logging/adapters/s3.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
upload
async
¶
upload(request_id, request_body, response_content)
Upload request and response bodies to S3.
Source code in majordomo_llm/logging/adapters/s3.py
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 | |
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 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 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 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 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 | |