> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Using Verbose Flag

> Explains enabling verbose logging in LangChain to inspect formatted prompts, lifecycle events, and outputs, how to set verbose True, use it selectively, and route logs via callbacks

In this lesson we'll demonstrate how to enable verbose logging for a specific LangChain component so you can inspect what happens during execution. This technique is useful for debugging prompt formatting, understanding chain lifecycle events, and routing runtime information to custom handlers.

We use an LLMChain with a ChatPromptTemplate that contains placeholders. First, here is the same program with verbose logging disabled.

```python theme={null}
from langchain.chains import LLMChain
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a {subject} teacher"),
    ("human", "Tell me about {concept}")
])

llm = ChatOpenAI()
chain = LLMChain(llm=llm, prompt=prompt)

result = chain.invoke({"subject": "physics", "concept": "galaxy"})
print(result)
```

Example output (non-verbose):

```python theme={null}
{
  'subject': 'physics',
  'concept': 'galaxy',
  'text': 'Galaxies are massive systems of stars, stellar remnants, interstellar gas, dust, dark matter, and other astronomical objects bound together by gravity. They come in a variety of shapes and sizes, ranging from dwarf galaxies with just a few billion stars to giant galaxies with hundreds of billions of stars.\n\nOur own Milky Way galaxy is a spiral galaxy, with a central bulge surrounded by a disk of stars, gas, and dust...'
}
```

## Enable verbose output for a chain

To print lifecycle messages and the fully formatted prompt (placeholders replaced), pass `verbose=True` when constructing the component:

```python theme={null}
from langchain.chains import LLMChain
from langchain_openai import ChatOpenAI
from langchain.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a {subject} teacher"),
    ("human", "Tell me about {concept}")
])

llm = ChatOpenAI()
chain = LLMChain(llm=llm, prompt=prompt, verbose=True)

result = chain.invoke({"subject": "physics", "concept": "galaxy"})
print(result)
```

Verbose console output (example):

```text theme={null}
> Entering new LLMChain chain...
Prompt after formatting:
System: You are a physics teacher
Human: Tell me about galaxy

> Finished chain.
{'subject': 'physics',
 'concept': 'galaxy',
 'text': 'A galaxy is a vast system of stars, stellar remnants, interstellar gas, dust, dark matter, and other celestial objects bound together by gravity. These systems can contain billions or even trillions of stars, as well as various types of celestial bodies such as planets, moons, asteroids, and comets.\n\nOur own galaxy, the Milky Way, is a barred spiral galaxy that contains an estimated 100-400 billion stars. Galaxies come in various shapes and sizes, including spiral, elliptical, and irregular...'}
```

## What the verbose flag exposes

Verbose is a targeted debugging aid that reveals key runtime information for a component. It typically provides:

|                                           What it prints | Why it helps                                                                |
| -------------------------------------------------------: | --------------------------------------------------------------------------- |
| Lifecycle messages (entering/finishing a chain or agent) | Shows when a component starts and ends, useful for timing and flow analysis |
|      The prompt after formatting (placeholders replaced) | Lets you verify the exact text sent to the LLM and catch template mistakes  |
|                                Response or final outputs | Confirms what the component returned and how it was processed               |

## Use verbose selectively

Enable `verbose=True` only on the component(s) you need to inspect. This focused approach avoids overwhelming output from other parts of your system while giving clear visibility into the component under investigation.

|    Component type | How to enable verbose                                 | Example                          |
| ----------------: | ----------------------------------------------------- | -------------------------------- |
|             Chain | Pass `verbose=True` to the chain constructor          | `LLMChain(..., verbose=True)`    |
|             Agent | Pass `verbose=True` to the agent when creating it     | `Agent(..., verbose=True)`       |
| Custom components | Implement and expose a verbose flag in your component | `MyComponent(..., verbose=True)` |

<Callout icon="lightbulb" color="#1CB2FE">
  Use `verbose=True` on only the components you need to inspect. Global debug or tracing options can produce a large amount of output; verbose is a targeted way to get helpful runtime visibility.
</Callout>

## Example input data

Below is the input dictionary used in the examples (shown in a code block to avoid MDX parsing issues):

```python theme={null}
{"subject": "physics", "concept": "galaxy"}
```

## Routing verbose output with callbacks

If you prefer structured logging or tracing, LangChain callbacks let you capture verbose events and route them to custom handlers (for example, sending logs to a monitoring system, or collecting them in a structured store). Use callbacks to integrate verbose output with your observability stack.

For more details, see:

* LangChain documentation: [https://langchain.readthedocs.io/](https://langchain.readthedocs.io/)
* ChatOpenAI: [https://pypi.org/project/langchain-openai/](https://pypi.org/project/langchain-openai/)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/langchain/module/b5f7771a-fdbc-45b1-a786-6c84bb7ffc76/lesson/ea0b058a-7c2d-4b17-81e6-fb4026f5a6fe" />
</CardGroup>
