data_pulse
Contributor

@Vamsi3757 

I experimented with a similar use case recently and built a working Spark job cost/performance optimization agent in Databricks.

I used a custom Python tool-calling agent with databricks-gpt-oss-120b through Databricks Model Serving, hosted as a Databricks App. I've attached the simplified architecture diagram for reference.

data_pulse_0-1792654350399.png

A few observations from the implementation:

Python tool orchestration: Exposed existing Python diagnostic logic directly as tools for retrieving job metrics, detecting anomalies, inspecting query history, and optionally analysing Spark event logs. The LLM dynamically selects tools, while Python validates and executes the calls. This worked without requiring LangGraph or converting the functions into Unity Catalog functions.

Native system tables: Used system.lakeflow.job_run_timeline, system.billing.usage, system.billing.list_prices, and system.query.history for run history, DBUs, estimated costs, and SQL execution metrics. These helps to gather all Job run time history, Cost, Queries ran as part of it.

Evidence-based recommendations: Implemented runtime/DBU anomaly detection using historical baselines, with safeguards for insufficient data. The agent distinguishes observations from possible causes and suggests further investigation rather than assuming a root cause.

Spark-level diagnostics: In a separate Photon shuffle experiment, Query Profile showed 70.5 MB of local shuffle activity, while system.query.history.shuffle_read_bytes reported zero network shuffle. This highlights why query level metrics alone may not capture all Spark execution details.

Spark event logs: Implemented Python parsing for stage/task-level diagnostics, currently validated with synthetic event-log fixtures. 

Conversational context: The deployed agent successfully handled follow up questions using previously validated evidence without necessarily re-executing all the diagnostic tools.

My takeaway: For this use case, a lightweight custom Python tool-calling agent is a practical starting point. Keep the diagnostic functions modular, use native system tables for historical cost/performance analysis and invoke detailed Spark event-log analysis selectively when available.

Genie could complement this for conversational exploration of curated metrics, but isn't essential for orchestrating Python diagnostic tools.

Below is from the App and its response.

data_pulse_1-1792654665167.png

Hope this helps and give some more insights. All the best for your implementation.