Sub topic 11
Seeing what the model was actually sent
This chapter assumes you can call a chat API, read JSON, and use Python virtual environments. You should also know the basics from the earlier chapters: messages, tool calls, and what a framework wraps around an SDK call.
Why the final prompt matters
When a model gives a bad answer, the cause is usually in the input. A framework may add a system prompt, reformat your messages, insert retrieved text, or append a tool schema. The text you wrote is not always the text the model received.
With a provider SDK, the request is the dictionary you pass in, so you can print it. With a framework, several layers build the request for you. Debugging then means finding the final payload, not the code you wrote.
The most reliable place to look is the HTTP request body (point D in the diagram). Every approach ends there, so it is the one view that works for all of them.
Option 1: log the request yourself
For a direct SDK call, log the messages before you send them.
import json
from openai import OpenAI
client = OpenAI()
messages = [
{"role": "system", "content": "You are a concise assistant."},
{"role": "user", "content": "Explain what an API client library is in one sentence."},
]
print(json.dumps(messages, indent=2))
response = client.chat.completions.create(model="gpt-4o-mini", messages=messages)
print(response.usage)
print(response.choices[0].message.content)response.usage reports prompt and completion token counts. If the prompt token count is much higher than you expect, something extra was sent.
Option 2: turn on library logging
The OpenAI Python SDK builds on httpx. Setting the OPENAI_LOG environment variable to debug makes it log its requests. LangChain and LlamaIndex also use the OpenAI SDK for OpenAI models, so this shows what they send too.
import logging
import os
os.environ["OPENAI_LOG"] = "debug"
logging.basicConfig(level=logging.DEBUG)
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o-mini")
print(llm.invoke("Say hello.").content)The debug output includes the request options, such as the JSON body with messages and model. The exact log format varies between library versions, so check what your installed version prints.
Option 3: framework tracing
Frameworks provide their own hooks.
| Tool | What it shows | Notes |
|---|---|---|
| LangChain callbacks and LangSmith | Each step in a chain, with inputs and outputs | LangSmith is a hosted service |
| LlamaIndex callbacks and instrumentation | Retrieved chunks, the prompt, and the response | Shows which chunks were retrieved |
| PydanticAI with Logfire | Messages, tool calls, and validation retries | Built on OpenTelemetry |
| A proxy or gateway | The raw HTTP traffic | Works with any library |
Tracing tools show structure, such as which step ran and how long it took. They may also show a prompt that is not the exact final one. Compare against the HTTP body when the details matter.
What to look for
- A system prompt you did not write.
- Retrieved chunks that are irrelevant, duplicated, or cut off.
- Tool schemas that are larger than expected, which adds tokens on every call.
- Chat history that grows with each turn.
- Retries after validation failures, which cost extra calls.
Exercise
A LangChain app returns answers that ignore your instructions, and the prompt token count is twice what you expect. Name two places to check and what you would look for.
Show answer
Turn on SDK debug logging or use a logging proxy to read the HTTP request body, then look for an extra system prompt, long chat history, or oversized tool schemas. Also check the framework trace (for example LangSmith) for retrieved chunks or template text that were inserted into the prompt.
Exercise
Why is the HTTP request body a better place to debug than a framework trace?
Show answer
Every approach ends with that request, so it shows exactly what the model received. A framework trace shows steps and may not show the final text.