We began our series on Generative AI with Retrieval Augmented Generation (RAG) , focusing on its applications in the energy sector .
In our first post, we explored Tavily Search as a web search tool integrated with LangChain .
While RAG is a powerful tool, more complex agentic workflows combine RAG with other agents and tools depending on specific needs.

In this blog-post we continue working with the Energy dataset from PJM Interconnection LLC, a regional transmission organization (RTO) in the United States. Serving all or parts of Delaware, Illinois, Indiana, Kentucky, Maryland, Michigan, New Jersey, North Carolina, Ohio, Pennsylvania, Tennessee, Virginia, West Virginia, and the District of Columbia.
We have scaled up the experiments to cover the files:
AEP_hourly.csvCOMED_hourly.csvDAYTON_hourly.csvDEOK_hourly.csvDOM_hourly.csvDUQ_hourly.csvEKPC_hourly.csvFE_hourly.csvNI_hourly.csvPJME_hourly.csvPJMW_hourly.csvOur dataset consists of datetime and consumption columns , making it ideal for an agentic workflow where LLMs generate code for analytics tasks .
In our previous RAG post, we observed that LLMs naturally generate Python code when given structured data.
To enhance control, we explicitly define system prompts with:
These prompts are then combined with user queries, allowing users to interact with the system in natural language—without any coding knowledge .
Our first version of this system is illustrated in Figure 1.

As shown in Figure 1 , the process begins when a user submits a query . The LLM interprets the request and returns an answer—often containing code.
We then:
To simplify querying, we leverage DuckDB , which allows us to use SQL directly on local datasets .
In Figure 2 , we show an example where GPT-4o generates an SQL query and its corresponding result.
User Query:
“Find the 7 highest peaks of consumption between December 1 and December 10, 2015.”

The generated SQL query is extracted, executed with DuckDB , and the results are returned.
While we find SQL-based querying effective , our focus in this blog post remains on Python-based coding agents .
One major advantage of our agentic pipeline is data privacy . Unlike traditional approaches where raw data is sent to an LLM for processing, our system ensures that:
Column_A ) to their real counterparts.By following this approach, organizations can leverage LLMs without violating privacy regulations such as GDPR and EU AI Act .
We conducted a simple experiment by asking the following questions:
YYYY ).YYYY ).These questions were applied to each year ( YYYY ) in all datasets listed above.
While some datasets span 2004–2018 , others have shorter time frames .
Each question was appended to our system prompt , and we reset the experiment before every iteration to simulate a cold start scenario .
Future improvements could include caching responses or adding memory to optimize the pipeline.

To evaluate the coding capabilities of LLMs, we tested models that were both correct and consistent while following instructions effectively. The models were divided into two categories:
Closed-Source Models
These models were accessed via APIs using LangChain , though other frameworks could also be used:
GPT-4oGPT-4o-miniMistral-Large-LatestOpen-Weight Models
These models were run locally using Ollama , imposing computational constraints:
LLaMA 3.1 8BCodeLlama 7BCodeLlama 13BMistral v3 7BPhi-4 14BDeepSeek-R1 8BDeepSeek-R1 14B (incomplete results)Among these, DeepSeek-R1 8B performed less accurately than its parent model, LLaMA 3.1 8B , from which it was distilled.
While DeepSeek-R1 14B achieved perfect accuracy in a single test , we abandoned further experiments due to high computational costs .
Similarly, Phi-4 14B performed well but was relatively slow on our local setup.
The primary evaluation metric was accuracy —whether the generated code produced the correct numerical result.
To compute accuracy: 1. We manually calculated gold-standard statistics for comparison. 2. Each LLM-generated solution was compared against these ground truths. 3. Accuracy was measured as a percentage of correct results across all dataset years .
Each dataset corresponds to a column in Tables 1–4 , with accuracy values averaged across all years.
The following models achieved 100% accuracy across all datasets:
✅ GPT-4o
✅ GPT-4o-mini
✅ Mistral-Large-Latest
✅ Phi-4 14B
✅ CodeLlama 13B
✅ LLaMA 3.1 8B
For other models, accuracy varied:
CodeLlama 7B made only one error .DeepSeek-R1 8B produced long, unnecessary reasoning and syntax errors , even for simple tasks.LLaMA 3.1 8B —the parent of DeepSeek-R1 8B —performed better but still made one error in a dataset.Challenges with Local Execution
A major disappointment was Mistral v3 7B , which consistently underperformed across datasets.




While integrating agentic AI workflows , we must consider several key risks and security challenges :
temperature=0 , responses may vary across runs.To minimize these risks, we implement the following safeguards:
✅ Prevent Harmful Code Execution
✅ Ensure Data Integrity
✅ Enforce Consistency
✅ Real-Time Monitoring & Logging
By implementing these guardrails , organizations can ensure secure, reliable, and scalable AI-powered workflows.
In this post, we demonstrated how agentic workflows can significantly improve data analytics in the energy sector by:
Stay tuned & follow us for updates!