Monitoring helps an enterprise keep a generative AI application reliable and performant after deployment. It continuously measures application health and supports decisions about scaling, optimization, and responsible operation. section 1.9 section 11.4.7
Monitoring is one part of observability, alongside logging and tracing. Monitoring measures performance indicators; logging records requests, responses, and errors; tracing follows requests through models and services to help locate bottlenecks. section 11.4.7 section 10.2.2
Track indicators that reflect application health and operating cost:
- Response time and throughput show how quickly the application responds and how much work it handles. section 11.4.7
- Error rates and resource utilization help identify failures and inform scaling decisions. section 11.4.7
- Token usage matters because it directly affects LLM API costs. section 11.5.1
Use alerts to identify anomalies, and plan for downtime and API limits. Combine continuous measurement with performance tests that simulate different load conditions; Prometheus can collect metrics, Grafana can visualize them, and JMeter or Locust can simulate user load. section 10.2.2 section 11.5.1
Measure a chat request
A console chat application can time a chat completion call and count prompt, conversation, and completion tokens. MLflow provides an experiment-tracking interface for observability, while the measurements help relate response latency and token usage to application behavior. section 11.4.7
Listing 11.4 MLflow observability example
import prometheus_client as prom
import mlflow
...
# Set OpenAI API key
API_KEY = os.getenv("OPENAI_API_BOOK_KEY")
MODEL = "gpt-3.5-turbo"
MLFLOW_URI = "http://localhost:5000"
...
# Initialize OpenAI client
client = OpenAI(api_key=API_KEY)
# Set MLflow tracking URI
mlflow.set_tracking_uri(MLFLOW_URI)
mlflow.set_experiment("GenAI_book")
def generate_text(conversation, max_tokens=100)->str:
start_time = time.time()
response = client.chat.completions.create(
model=MODEL,
messages=conversation,
)
latency = time.time() - start_time
message_response = response.choices[0].message.content
# Count tokens in the prompt, and the completion
prompt_tokens = count_tokens(conversation[-1]['content'])
conversation_tokens = count_tokens(str(conversation))
completion_tokens = count_tokens(message_response)
# Log metrics using MLflow
with mlflow.start_run():
mlflow.log_metrics({
"request_count": 1,
"request_latency": latency,
"prompt_tokens": prompt_tokens,
"completion_tokens": completion_tokens,
"conversation_tokens": conversation_tokens
})
mlflow.log_params({
"model": MODEL,
"temperature": TEMPERATURE,
"top_p": TOP_P,
"frequency_penalty": FREQUENCY_PENALTY,
"presence_penalty": PRESENCE_PENALTY
})
return message_response
if __name__ == "__main__":
conversation = [{"role": "system", "content":
"You are a helpful assistant."}]
while True:
user_input = input(f"You: ")
conversation.append({"role": "user", "content": user_input})
output = generate_text(conversation, 256)
print_ai_output(output)
conversation.append({"role": "assistant", "content": output})
Inspect the chat completion call, latency measurement, and token counts in the MLflow observability example. section 11.4.7
Detailed prompts and model outputs can help debug behavior, but they may contain sensitive data. Apply data minimization, encryption, access controls, and relevant regulatory requirements when designing logging and monitoring. Continue testing and fine-tuning after deployment rather than treating release as the end of the work. section 11.4.7 section 10.2.2 section 1.9








































