Observability helps production LLM applications work as intended by using operational data to understand behavior and diagnose problems. It combines monitoring, logging, and tracing to support reliability, performance, and user experience. section 11.4.7 section 11.7
Use the three components together:
- Monitoring measures indicators such as response time, throughput, error rates, and resource utilization. Use them to assess application health and guide scaling and optimization. section 11.4.7
- Logging records requests and responses, including prompts and model outputs. These records help debug problems and understand model behavior. section 11.4.7
- Tracing follows requests through the application. It is especially useful for locating bottlenecks across complex architectures or multiple models and services. section 11.4.7
Measure a chat completion
A Python chat application uses MLflow alongside a chat completion API. It times the API call and counts prompt, conversation, and completion tokens, providing measurements for investigating latency and usage. 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})
The listing shows the tracking setup, API call, latency measurement, and token counts used in the example. section 11.4.7
MLflow, Traceloop, and Prompt flow are options for application observability. For inference monitoring, also consider time to first token and time per output token alongside latency and throughput. Because logs can contain prompts and outputs, apply production security controls such as encryption and access control. section 11.7 section 11.4.7















