Skip to main content
The Gradio Chatbot can natively display intermediate thoughts and tool usage in a collapsible accordion next to a chat message. This makes it perfect for creating UIs for LLM agents and chain-of-thought (CoT) or reasoning demos. This guide will show you how to display thoughts and tool usage with gr.Chatbot and gr.ChatInterface. Agent thoughts example

The ChatMessage dataclass

Every element of the chatbot value is a dictionary of role and content keys. You can always use plain Python dictionaries to add new values to the chatbot but Gradio also provides the ChatMessage dataclass to help you with IDE autocompletion. The schema of ChatMessage is as follows:
For our purposes, the most important key is the metadata key, which accepts a dictionary. If this dictionary includes a title for the message, it will be displayed in a collapsible accordion representing a thought. It’s that simple! Take a look at this example:
In addition to title, the dictionary provided to metadata can take several optional keys:
  • log: an optional string value to be displayed in a subdued font next to the thought title
  • duration: an optional numeric value representing the duration of the thought/tool usage, in seconds. Displayed in a subdued font inside parentheses next to the thought title
  • status: if set to "pending", a spinner appears next to the thought title and the accordion is initialized open. If status is "done", the thought accordion is initialized closed. If status is not provided, the thought accordion is initialized open and no spinner is displayed
  • id and parent_id: if these are provided, they can be used to nest thoughts inside other thoughts
Below, we show several complete examples of using gr.Chatbot and gr.ChatInterface to display tool use or thinking UIs.

Building with agents

A real example using transformers.agents

We’ll create a Gradio application with a simple agent that has access to a text-to-image tool.
Make sure you read the smolagents documentation first.
We’ll start by importing the necessary classes from transformers and gradio:
Then we’ll build the UI:
You can see the full demo code here.

A real example using LangChain agents

We’ll create a UI for a LangChain agent that has access to a search engine. We’ll begin with imports and setting up the LangChain agent. Note that you’ll need an .env file with the following environment variables set:
Then we’ll create the Gradio UI:
That’s it! See our finished LangChain demo here.

Building with thinking LLMs

The Gradio Chatbot can natively display intermediate thoughts of a thinking LLM. This makes it perfect for creating UIs that show how an AI model “thinks” while generating responses. Below guide will show you how to build a chatbot that displays Gemini AI’s thought process in real-time.

A real example using Gemini 2.0 Flash Thinking API

Let’s create a complete chatbot that shows its thoughts and responses in real-time. We’ll use Google’s Gemini API for accessing Gemini 2.0 Flash Thinking LLM and Gradio for the UI. We’ll begin with imports and setting up the Gemini client. Note that you’ll need to acquire a Google Gemini API key first:
First, let’s set up our streaming function that handles the model’s output:
Then, let’s create the Gradio interface:
This creates a chatbot that:
  • Displays the model’s thoughts in a collapsible section
  • Streams the thoughts and final response in real-time
  • Maintains a clean chat history
You now have a chatbot that not only responds to users but also shows its thinking process, creating a more transparent and engaging interaction. See our finished Gemini 2.0 Flash Thinking demo here.

Building with citations

The Gradio Chatbot can display citations from LLM responses, making it perfect for creating UIs that show source documentation and references. This guide will show you how to build a chatbot that displays Claude’s citations in real-time.

A real example using Anthropic’s Citations API

Let’s create a complete chatbot that shows both responses and their supporting citations. We’ll use Anthropic’s Claude API with citations enabled and Gradio for the UI. We’ll begin with imports and setting up the Anthropic client. Note that you’ll need an ANTHROPIC_API_KEY environment variable set:
First, let’s set up our message formatting functions that handle document preparation:
Then, let’s create our bot response handler that processes citations:
Finally, let’s create the Gradio interface:
This creates a chatbot that:
  • Supports both plain text and PDF documents for Claude to cite from
  • Displays citations in collapsible sections using our metadata feature
  • Shows source quotes directly from the given documents
The citations feature works particularly well with the Gradio Chatbot’s metadata support, allowing us to create collapsible sections that keep the chat interface clean while still providing easy access to source documentation. You now have a chatbot that not only responds to users but also shows its sources, creating a more transparent and trustworthy interaction. See our finished Citations demo here.