How Do You Debug a Failed Pipeline Run Across Airflow, dbt and Snowflake in One Place?
A five-layer method for joining three tools into one timeline with a shared run key
To debug a failed run across Airflow, dbt and Snowflake in one place, stamp one run key into every query the run issues, then join the three record sets on that key. dbt's query-comment config writes metadata into the SQL text of every statement, and Snowflake's QUERY_HISTORY view returns that text with start times, status and error messages. Join those on the Airflow run ID and you get one ordered timeline instead of three tabs.
Key Takeaways
- •The fix is a shared run key: the Airflow run ID, passed to dbt as an environment variable and written into every Snowflake query through a dbt query-comment macro.
- •Snowflake's ACCOUNT_USAGE QUERY_HISTORY view keeps 365 days of history but can lag by up to 45 minutes, so recent failures need the INFORMATION_SCHEMA table function instead.
- •A good timeline has three lanes, orchestrator, transformation and warehouse, sorted by timestamp on one clock.
- •Correlation is mechanical work, so an agent can do it; deciding whether to rerun, backfill or roll back should stay with a person until the fix has a track record.
- •Practitioners describe the pain of jumping between Airflow, dbt Cloud and Snowflake to debug one failed run, and the shared key removes most of that hopping.
Why debugging across three tools is slow
Each tool records the same failure from a different angle. Airflow tells you a task exited non-zero and shows the tail of its log. dbt tells you which model failed and prints the compiled SQL. Snowflake tells you the query that actually ran, how long it queued, and the exact error code. None of the three stores the other two's identifiers, so the engineer joins them by hand using timestamps and guesswork.
The guesswork is where time goes. A DAG run that touches forty models can issue hundreds of queries in the same window as other jobs on the same warehouse. Matching an Airflow task's failure time to the right Snowflake query by eyeballing start times works until two jobs fail in the same five minutes, and then it quietly produces the wrong root cause.
A practitioner thread on the dbt community forum describes exactly this loop, stopping the jumping between Airflow, dbt Cloud and Snowflake to debug a failed run. We agree with the framing. The tools are fine individually; the missing piece is an identifier that survives the hand-offs between them.
Layer 1: Pick one run key and pass it down
Start with the Airflow run ID. It is unique per DAG run, human-readable, and available as a template variable, as the Airflow templates reference documents. Combine it with the task ID if you want per-task resolution. Do not invent a new ID system; reusing the orchestrator's own identifier means anyone can paste it from the Airflow UI and start the investigation.
Pass the key to dbt as an environment variable on the task that invokes it. With a BashOperator or a containerised dbt task, set env={"DW_RUN_KEY": "{{ run_id }}", "DW_TASK_ID": "{{ task.task_id }}"}. Both fields are templated, so Airflow renders them per run. The dbt process now knows which orchestrator run it belongs to, which is the one fact the warehouse cannot learn by itself.
Keep the key short and free of characters that break SQL comments. Airflow run IDs contain colons and plus signs, which are fine inside a JSON string in a comment. If you run dbt Cloud jobs instead, the same idea applies: trigger the job through the API and record the dbt Cloud run ID next to the Airflow run ID in a small mapping table.
Layer 2: Write the key into every Snowflake query with a dbt macro
dbt already appends a comment to each statement it sends; the query-comment documentation describes the default content, which includes the dbt version, profile, target and node ID. Override it with a macro that adds your run key. In dbt_project.yml set query-comment: {comment: "{{ query_comment(node) }}", append: true} and define the macro below in macros/query_comment.sql.
sql
{% macro query_comment(node) %}
{%- set c = {
'app': 'dbt',
'invocation_id': invocation_id,
'airflow_run_id': env_var('DW_RUN_KEY', 'none'),
'airflow_task_id': env_var('DW_TASK_ID', 'none'),
'node_id': node.unique_id if node else 'none'
} -%}
{{ return(tojson(c)) }}
{% endmacro %}
The append: true setting matters on Snowflake, because a leading comment can be stripped from the stored query text in some tooling. Appending keeps the JSON at the end of the statement where a LIKE filter finds it reliably. Run dbt compile and open a compiled file's logs to confirm the comment appears, then run one model and check it in Snowflake before you roll the change out across the project.
If you also want session-level tagging, Snowflake supports a QUERY_TAG session parameter, documented in the parameters reference. It is a separate column in QUERY_HISTORY and easier to filter than text, but it covers a whole session rather than each model, so we use the comment for node-level detail and the tag for run-level filtering.
Layer 3: Pull the Snowflake side with a QUERY_HISTORY filter
With the key in the SQL text, the warehouse side becomes one query. Snowflake documents that the ACCOUNT_USAGE view has up to 45 minutes of latency and retains 365 days of history. That is ideal for post-mortems on yesterday's run and wrong for a failure that happened ten minutes ago, where you should use the INFORMATION_SCHEMA.QUERY_HISTORY table function in the database you ran against.
sql
select query_id, start_time, end_time, warehouse_name,
execution_status, error_code, error_message,
queued_overload_time, total_elapsed_time
from snowflake.account_usage.query_history
where start_time > dateadd('hour', -12, current_timestamp())
and query_text ilike '%"airflow_run_id": "scheduled__2026-10-27%'
order by start_time;
Always bound the filter on start_time. Without it the scan covers the full retention window and gets slow and expensive. Read execution_status first: a model that failed on a compilation or permission error looks nothing like one that was cancelled after a long queue. The queued_overload_time column answers the question that Airflow cannot, which is whether the task was slow because of its own SQL or because the warehouse was saturated.
Layer 4: Join Airflow and dbt records onto the same clock
Now collect the other two lanes. From Airflow, export the task instances for the run: task ID, state, start, end, try number. From dbt, read target/run_results.json for the invocation, which lists each node's status, timing and message. Store the file from the failed attempt before a retry overwrites it. A one-line upload to object storage at the end of the task, using on_failure_callback, is enough.
Normalise timestamps to UTC before joining. Airflow shows times in the UI's configured timezone, dbt writes UTC, and Snowflake returns the session timezone, which is the usual source of an apparent one-hour gap that is not real. Convert all three to UTC, tag each row with its lane, and union them into one ordered list. A failing timeline then reads top to bottom: task started, model started, query queued, query failed, model errored, task failed.
Read the timeline from the bottom up. The last event is the symptom Airflow showed you. The first event with an error state is usually the cause. In our experience the gap between those two is the useful part: a retry that hid a first failure, or a downstream model that failed only because its parent was skipped.
Layer 5: Let an agent assemble it, keep the decision with a person
Layers 1 to 4 are mechanical: find the run, pull three record sets, join, sort. That is the work our Incidents Agent does when it traces a failure across lineage, logs and queries. Every Data Workers agent is an MCP server, so you can ask from your terminal, for example "why did the nightly pipeline fail?", and get the timeline back. The Model Context Protocol specification defines the tool interface that makes this work in any compatible client.
We do not recommend letting an agent decide the remedy on day one. Rerunning a task, backfilling a partition, or rolling back a model each carry different risk depending on the table. Start with the agent proposing a fix and a person approving it, then widen the autonomy dial per domain as the proposals prove right. The read-only OSS edition is a safe place to start, since it can build timelines without any write access.
What this method does not solve
The shared key only helps for queries that carry it. Ad hoc queries, BI tool refreshes and other jobs that touch the same tables will not have the comment, so a failure caused by someone else's long-running query shows up as queue time with no owner. Check warehouse_name and the overlapping time window for those cases.
It also depends on discipline. A new dbt project that skips the macro, or an operator that forgets to pass the environment variable, produces runs with none in the key fields. A simple audit query that counts queries with airflow_run_id equal to none per day catches drift early and costs almost nothing.
Finally, the join explains what happened, not why a source system changed. If an upstream schema change broke a model, the timeline shows the failed query and the error, and you still need lineage to find the cause. That is where combining the timeline with schema-change history pays off.
Frequently Asked Questions
Can I do this without changing dbt?
Partly. You can set QUERY_TAG from the Airflow side through session parameters on the Snowflake connection, then filter QUERY_HISTORY on the tag. You lose per-model detail, because the tag covers the whole session, but you still get a run-level view of every query the task issued. Add the macro later when you need node-level resolution.
Does this work with dbt Cloud?
Yes, with one extra step. dbt Cloud jobs run outside your Airflow workers, so the environment variable cannot be passed directly. Set the run key as a job-level environment variable override when you trigger the job through the API, and keep the same macro in the project. Then record the dbt Cloud run ID alongside the Airflow run ID for the cross-reference.
Why is a recent failure missing from QUERY_HISTORY?
The ACCOUNT_USAGE view can lag, with Snowflake documenting up to 45 minutes of latency. For anything newer, query the INFORMATION_SCHEMA table function, which returns recent history without that delay but covers a much shorter window. Use the table function for live incidents and the view for post-mortems.