Skip to main content

BigQuery Callback Handler

CommunityPythonPreview
Google BigQuery is a serverless and cost-effective enterprise data warehouse that works across clouds and scales with your data.
The BigQueryCallbackHandler allows you to log events from LangChain and LangGraph to Google BigQuery. This is useful for monitoring, auditing, and analyzing the performance of your LLM applications. Key features:
  • LangGraph support: automatic detection of LangGraph nodes with AGENT_STARTING / AGENT_COMPLETED events and top-level INVOCATION_STARTING / INVOCATION_COMPLETED boundaries (vocabulary aligned with Google ADK’s BigQueryAgentAnalyticsPlugin)
  • Auto-created analytics views: one typed-column CREATE OR REPLACE VIEW per event type (v_llm_response.usage_total_tokens instead of JSON_VALUE(...))
  • Auto schema upgrade: additive ALTER TABLE ADD COLUMN on existing tables, gated by a schema-version label so it runs at most once per version
  • Sub-agent attribution: the agent BigQuery column is auto-derived from langgraph_node when no explicit agent is set in metadata, so multi-agent graphs are tagged per sub-agent without any user changes
  • Rich LLM telemetry: token usage (prompt_tokens / completion_tokens / total_tokens / cached_content_token_count), model_version, full usage_metadata, cache_metadata, plus llm_config (temperature, top_p, …) and tools on LLM_REQUEST
  • Latency tracking: built-in latency measurement for all LLM and tool calls
  • Event filtering: configurable allowlist / denylist plus an opt-in skip_internal_chain_events heuristic that drops noisy framework chains (ChannelWrite, RunnableLambda, …) without breaking trace continuity
  • Graph context manager: explicit INVOCATION_* boundaries with accurate timing
  • flush() between requests: drain the queue without tearing the handler down
  • Real-time dashboard: FastAPI monitoring webapp with live event streaming
Preview releaseThe BigQuery Callback Handler is in Preview. APIs and functionality are subject to change. For more information, see the launch stage descriptions.
BigQuery Storage Write APIThis feature uses the BigQuery Storage Write API, which is a paid service. For information on costs, see the BigQuery documentation.

Installation

You need to install langchain-google-community with bigquery extra dependencies. For this example, you will also need langchain-google-genai and langgraph.

Prerequisites

  1. Google Cloud Project with the BigQuery API enabled.
  2. BigQuery Dataset: Create a dataset to store logging tables before using the callback handler. The callback handler automatically creates the necessary events table within the dataset if the table does not exist.
  3. Google Cloud Storage Bucket (Optional): If you plan to log multimodal content (images, audio, etc.), creating a GCS bucket is recommended for offloading large files.
  4. Authentication:
    • Local: Run gcloud auth application-default login.
    • Cloud: Ensure your service account has the required permissions.

IAM Permissions

For the callback handler to work properly, the principal (e.g., service account, user account) under which the application is running needs these Google Cloud roles:
  • roles/bigquery.jobUser at Project Level to run BigQuery queries.
  • roles/bigquery.dataEditor at Table Level to write log/event data.
  • If using GCS offloading: roles/storage.objectCreator and roles/storage.objectViewer on the target bucket.

Use with LangGraph agent

To use the BigQueryCallbackHandler with a LangGraph agent, instantiate it with your Google Cloud project ID and dataset ID. The handler creates the events table (and per-event-type analytics views) on first run. Use the graph_context() method to track top-level invocation boundaries — it emits INVOCATION_STARTING on enter and INVOCATION_COMPLETED (or INVOCATION_ERROR on exception) with accurate latency. Pass session_id, user_id, and (optionally) agent via the metadata dictionary in the config object when invoking the agent. If agent is not set, the handler auto-derives it from metadata['langgraph_node'] so each sub-agent’s events are correctly attributed.

Configuration options

You can customize the callback handler using BigQueryLoggerConfig.
enabled
bool
default:"True"
To disable the handler from logging data to the BigQuery table, set this parameter to False.
clustering_fields
List[str]
default:"['event_type', 'agent', 'user_id']"
The fields used to cluster the BigQuery table when it is automatically created.
gcs_bucket_name
str
default:"None"
The name of the GCS bucket to offload large content (images, blobs, large text) to. If not provided, large content may be truncated or replaced with placeholders.
connection_id
str
default:"None"
The BigQuery connection ID (e.g., us.my-connection) to use as the authorizer for ObjectRef columns. Required for using ObjectRef with BigQuery ML.
max_content_length
int
default:"512000"
(500 KB) The maximum length (in characters) of text content to store inline in BigQuery before offloading to GCS (if configured) or truncating.
batch_size
int
default:"1"
The number of events to batch before writing to BigQuery.
batch_flush_interval
float
default:"1.0"
The maximum time (in seconds) to wait before flushing a partial batch.
shutdown_timeout
float
default:"10.0"
Seconds to wait for logs to flush during shutdown.
event_allowlist
List[str]
default:"None"
A list of event types to log. If None, all events are logged except those in event_denylist.
event_denylist
List[str]
default:"None"
A list of event types to skip logging.
log_multi_modal_content
bool
default:"True"
Whether to log detailed content parts (including GCS references).
table_id
str
default:"agent_events"
The default table ID to use if not explicitly provided to the callback handler constructor.
retry_config
RetryConfig
default:"RetryConfig()"
Configuration for retry logic (max retries, delay, multiplier) when writing to BigQuery fails.
queue_max_size
int
default:"10000"
The maximum number of events to hold in the internal buffer queue before dropping new events.
skip_internal_chain_events
bool
default:"False"
When True, drop CHAIN_* events emitted by framework-internal Runnables (ChannelWrite, ChannelRead, Branch, RunnableLambda, RunnableSequence, RunnableParallel, RunnableAssign, RunnablePassthrough, RunnableBinding, Pregel, __start__, __end__). Skipped runs are still registered in the trace registry so child LLM/tool events keep the real graph root as their trace_id (no broken traces). Each suppression logs a DEBUG line so the heuristic is auditable.
custom_tags
dict[str, Any]
default:"{}"
Static tags written to attributes.custom_tags on every event row. Useful for slicing dashboards by deployment, cohort, or experiment (e.g. {"env": "prod", "agent_role": "sales"}).
log_session_metadata
bool
default:"True"
When True, dumps the user-supplied RunnableConfig metadata (minus keys we already promote to first-class columns like session_id, user_id, agent, langgraph_node) under attributes.session_metadata.
content_formatter
Callable[[Any, str], Any]
default:"None"
Optional (raw_content, event_type) -> formatted hook invoked before content parsing. Useful for PII redaction or coercing custom payloads. Failures fall back to raw content with a warning — the formatter cannot break the agent.
auto_schema_upgrade
bool
default:"True"
When True, additively ALTER TABLE ADD COLUMN any new fields that future versions of this handler add to the events schema. Gated by a langchain_bq_schema_version table label so the diff runs at most once per schema version. Never drops, renames, or retypes columns.
create_views
bool
default:"True"
When True, automatically CREATE OR REPLACE per-event-type analytics views beside the events table. Each view unnests the JSON columns into typed top-level columns (see Auto-created analytics views below).
view_prefix
str
default:"v"
Prefix for auto-created view names (v_llm_request, v_tool_completed, …). Set per-table when several handler instances share one dataset to avoid collisions.
The following code sample shows how to define a configuration for the BigQuery callback handler with event filtering:

Schema and production setup

The plugin automatically creates the table if it does not exist. However, for production, we recommend creating the table manually using the following DDL, which utilizes the JSON type for flexibility and REPEATED RECORDs for multimodal content. Recommended DDL:

Auto-created analytics views

When the handler creates the events table, it also creates one CREATE OR REPLACE VIEW per event type beside it (controlled by create_views, default True). Each view unnests the JSON columns into typed top-level columns so analytics queries don’t have to spell JSON_VALUE(...) every time:
The default view names (configurable via view_prefix) and the typed columns each one adds on top of the always-included columns: Every view also exposes the always-included columns from the raw table (timestamp, event_type, agent, session_id, invocation_id, user_id, trace_id, span_id, parent_span_id, status, error_message, is_truncated) plus three columns lifted from the attributes JSON: root_agent_name, custom_tags, session_metadata.

Auto schema upgrade

Existing tables are auto-upgraded additively when the handler’s schema gains new columns. The handler reads the table at startup and runs ALTER TABLE ADD COLUMN for any new fields, gated by a langchain_bq_schema_version table label so the diff runs at most once per schema version. Never drops, renames, or retypes columns. Disable with auto_schema_upgrade=False.

Sub-agent attribution

For multi-agent LangGraph deployments, the agent BigQuery column is auto-derived from this fallback chain:
  1. metadata["agent"] — explicit user-supplied value (highest priority)
  2. metadata["langgraph_node"] — the active LangGraph node, so each sub-agent’s events are tagged with the node name
  3. metadata["checkpoint_ns"] — LangGraph checkpoint namespace
  4. handler.graph_name — fallback for top-level INVOCATION_* events
A multi-agent graph (e.g. supervisor → TheCritic, TheMeteo, …) thus produces telemetry where each event is attributed to the originating sub-agent without any user changes.

Event types and payloads

The content column contains a JSON object specific to the event_type. The content_parts column provides a structured view of the content, especially useful for images or offloaded data.
Content Truncation
  • Variable content fields are truncated to max_content_length (configured in BigQueryLoggerConfig, default 500KB).
  • If gcs_bucket_name is configured, large content is offloaded to GCS instead of being truncated, and a reference is stored in content_parts.object_ref.
content always carries a summary keyEvery event row’s content JSON object includes a summary string with a human-readable preview of the payload (capped at max_content_length). The summary is omitted from the per-event tables below to keep the shapes readable, but it is always present on disk.

LLM interactions

These events track the raw requests sent to and responses received from the LLM.

Sub-agent (LangGraph node) and invocation lifecycle

These events come from LangGraph’s node and graph-context lifecycle. agent is auto-derived from metadata['langgraph_node'] when no explicit agent is set, so events are tagged per sub-agent.

Tool usage

These events track the execution of tools by the agent. The tool name is also surfaced in attributes.tool_name and (for the auto-views) as a typed tool_name column.

Chain execution

These events fire for non-graph LangChain Runnable lifecycles (graph invocations and LangGraph nodes use the INVOCATION_* / AGENT_* events listed above instead).

Retriever usage

These events track the execution of retrievers.

Agent Actions

These events come from legacy LangChain AgentExecutor-style agents (on_agent_action / on_agent_finish). The data field contains a JSON-serialized string of the action / finish payload.

Other Events

Advanced analysis queries

Once your agent is running and logging events, you can perform power analysis on the agent_events table.

1. Reconstruct a Trace (Conversation Turn)

Use the trace_id to group all events (Chain, LLM, Tool) belonging to a single execution flow.

2. Analyze LLM Latency & Token Usage

Calculate the average latency and total token usage for your LLM calls.

3. Analyze Multimodal Content with BigQuery Remote Model (Gemini)

If you are offloading images to GCS, you can use BigQuery ML to analyze them directly.

4. Analyze Span Hierarchy & Duration

Visualize the execution flow and performance of your agent’s operations (LLM calls, Tool usage) using span IDs.

5. Querying Offloaded Content (Get Signed URLs)

6. Advanced SQL Scenarios

These advanced patterns demonstrate how to sessionize data, analyze tool usage, and perform root cause analysis using BigQuery ML.

Conversational Analytics in BigQuery

Conversational AnalyticsYou can also use BigQuery Conversational Analytics to analyze your agent logs using natural language. Just ask questions like:
  • “Show me the error rate over time”
  • “What are the most common tool calls?”
  • “Identify sessions with high token usage”

Looker Studio Dashboard

You can visualize your agent’s performance using our prebuilt Looker Studio Dashboard template. To connect this dashboard to your own BigQuery table, use the following link format, replacing the placeholders with your specific project, dataset, and table IDs:

LangGraph integration

The BigQueryCallbackHandler provides enhanced support for LangGraph agents with automatic node detection, graph-level tracking, and latency measurements.

LangGraph event types

In addition to standard LangChain events, the callback handler automatically detects and logs LangGraph-specific events:

Graph context manager

Use the graph_context() method to explicitly mark graph execution boundaries. This enables INVOCATION_STARTING and INVOCATION_COMPLETED events with accurate latency measurements:

Latency tracking

The callback handler automatically tracks latency for all operations and stores measurements in the latency_ms JSON column:

Event filtering

Use event_allowlist and event_denylist to control which events are logged:
Or exclude noisy events:

Examples and resources

Example code

The following examples demonstrate various features of the BigQuery callback handler:

Analytics notebook

The LangGraph Agent Analytics notebook provides comprehensive BigQuery analytics queries for:
  • Real-time event monitoring
  • Tool usage analytics
  • Latency analysis and trends
  • Error debugging
  • User engagement metrics
  • Time-series visualization

Real-time monitoring dashboard

A FastAPI-based monitoring dashboard is available for real-time agent monitoring: Features:
  • Live event stream via Server-Sent Events (SSE)
  • Interactive charts for event distribution and latency trends
  • Session tracing with detailed timeline view
  • 20+ REST API endpoints for analytics queries
  • Auto-refresh every 5 seconds

Feedback

We welcome your feedback on BigQuery Agent Analytics. If you have questions, suggestions, or encounter any issues, please reach out to the team at bqaa-feedback@google.com.

Additional resources