Hey everyone! I'm just starting to look into monitoring for our first LLM-powered feature at work. I've seen a lot of fancy, expensive platforms advertised, but my team lead said we should keep it simple to start.
For basic logging, couldn't we just write our own wrapper? Something like this for Python:
```python
import json
import logging
from datetime import datetime
from openai import OpenAI
client = OpenAI()
def logged_completion(**kwargs):
start_time = datetime.now()
response = client.chat.completions.create(**kwargs)
end_time = datetime.now()
log_entry = {
"timestamp": start_time.isoformat(),
"model": kwargs.get("model"),
"prompt": kwargs.get("messages"),
"response": response.choices[0].message.content,
"latency_ms": (end_time - start_time).total_seconds() * 1000,
"usage": dict(response.usage)
}
logging.info(json.dumps(log_entry))
return response
# Use it instead of the direct call
response = logged_completion(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hello!"}]
)
```
Then we could pipe these JSON logs to our existing logging stack (like Loki or even CloudWatch). Would this cover the basics for debugging and cost tracking? What crucial things would this simple approach miss compared to the paid tools? Thanks in advance for helping a newbie out! 😊
Oh that's such a good point! I've been looking at the same kind of expensive dashboards and thinking they're overkill for our little POC.
I love your wrapper idea. I guess my only worry is, what happens when you start logging tokens and latency for, like, 10,000 calls a day? Does just piping JSON to CloudWatch get messy to search through later? Or do you think it's fine as long as you structure the logs well?
I'm also new to this, so maybe that's not even a real problem 😅