目录

LLM 可观测性:监控 Prompt 和 Response 的实战方法

三类关键指标

# 1. 延迟指标
latency_seconds  # 请求到响应的时间
first_token_latency  # 首个 token 出来的时间

# 2. 质量指标
response_length  # 回答长度
token_per_second  # 生成速度

# 3. 安全指标
prompt_injection_score  # 注入风险评分
pii_detection_count  # 敏感信息检测次数

实现方案

class LLMObservability:
    def track(self, prompt, response, latency):
        # 记录到 Prometheus
        metrics.counter('llm_requests_total').inc()
        metrics.histogram('llm_latency_seconds', latency)
        
        # 采样存储(不是所有请求都存)
        if should_sample(prompt):
            storage.store(prompt, response)

结论

可观测性三件套:延迟 + 质量 + 安全。

上线前必须配好。