liveBook · Manning

Observability

As explained in Generative AI in Action

Observability is the practice of monitoring, logging, and tracing a system to understand its internal behavior and diagnose problems from its outputs and operational data.

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

How Generative AI in Action covers it

How deep it goes
Defines monitoring, logging, and tracing, then illustrates implementation with tools and a chat application.
Examples
Uses a Python chat-completion example, an MLflow and Prometheus configuration, and a Traceloop load test.
What you can do after
Apply it via MLflow to measure chat-completion latency and count tokens.
How it connects
Connects observability with LLMOps, inference metrics, scalability, security, and compliance.

Also explained in

Compare how 16 books explain observability →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Generative AI in Action. The section links point to the parts of the book it draws on.