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.

When an LLM application moves into production, a successful API response is not enough to show that the whole system is working well. Observability combines monitoring, logging, and tracing so teams can understand application behavior, find problems, and make informed scaling and optimization decisions. section 11.4.7 section 11.0

Use three complementary views:

  • Monitor response times, throughput, error rates, and resource utilization to assess application health. section 11.4.7
  • Log requests and responses, including prompts and model outputs, to investigate behavior and debug failures. section 11.4.7
  • Trace a request across models and services to locate bottlenecks in a complex application. section 11.4.7

Instrument a chat-completion call

A Python chat example sends requests to an OpenAI client and measures the elapsed time around each completion. It also counts prompt, conversation, and completion tokens. MLflow provides experiment tracking for examining the resulting metrics and prompt-and-response details. The example is a starting point for instrumentation, not a complete production monitoring setup. 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 where the chat call is timed and token counts are collected, making the measurement points concrete. section 11.4.7

Choose tooling to match the question: MLflow supports tracking and monitoring, while Traceloop and Prompt flow are also presented as observability options. Treat captured prompts and outputs as a security and compliance consideration; production deployment calls for access controls and encryption alongside observability. section 11.4.7 section 11.7

How Generative AI in Action covers it

How deep it goes
Defines monitoring, logging, and tracing, then illustrates instrumentation with tools rather than developing a full production monitoring procedure.
Examples
Uses a timed chat-completion call, token counts, MLflow dashboards, and a Traceloop example.
What it assumes
Assumes familiarity with LLM application calls while defining monitoring, logging, and tracing in plain operational terms.
What you can do after
Apply it via MLflow, Traceloop, or Prompt flow to monitor, log, and trace an LLM application.
How it connects
Connects observability to LLMOps, inference metrics, latency, 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.