Paper deep dive
Agentic Application in Power Grid Static Analysis: Automatic Code Generation and Error Correction
Qinjuan Wang, Shan Yang, Yongli Zhu
Intelligence
Status: succeeded | Model: google/gemini-3.1-flash-lite-preview | Prompt: intel-v1 | Confidence: 98%
Last extracted: 4/14/2026, 1:59:17 AM
Summary
This paper presents an LLM-based agent framework for automating power grid static analysis by converting natural language instructions into MATPOWER scripts. The system utilizes DeepSeek-OCR for structured document processing, an enhanced RAG-based vector database, and a three-tier error-correction mechanism (static pre-check, dynamic feedback loop, and semantic validator) to ensure code fidelity and eliminate hallucinations. Experimental results show an 82.38% Global CSGF Accuracy (GCA).
Entities (6)
Relation Signals (4)
MATPOWER Agent â executescodein â MATLAB
confidence 100% ¡ The agent launches a MATLAB engine session through the MATLAB Executor module.
MATPOWER Agent â implements â Model Context Protocol
confidence 100% ¡ The system implements the MCP standard and packages the entire function into an MCP server.
MATPOWER Agent â uses â DeepSeek-OCR
confidence 100% ¡ The framework utilizes DeepSeek-OCR to build an enhanced vector database from MATPOWER manuals.
LangChain â integrates â DeepSeek
confidence 95% ¡ The system uses the LangChain framework as its brain and wraps the DeepSeek interface.
Cypher Suggestions (0)
No Cypher suggestions yet.
Abstract
Abstract:This paper introduces an LLM agent that automates power grid static analysis by converting natural language into MATPOWER scripts. The framework utilizes DeepSeek-OCR to build an enhanced vector database from MATPOWER manuals. To ensure reliability, it devises a three-tier error-correction system: a static pre-check, a dynamic feedback loop, and a semantic validator. Operating via the Model Context Protocol, the tool enables asynchronous execution and automatically debugging in MATLAB. Experimental results demonstrate that the system achieves a 82.38% accuracy regarding the code fidelity, effectively eliminating hallucinations even in complex analysis tasks.
Tags
Links
- Source: https://arxiv.org/abs/2604.09995v1
- Canonical: https://arxiv.org/abs/2604.09995v1
Trouble viewing inline? Open PDF directly â
Full Text
24,592 characters extracted from source content.
Expand or collapse full text
Agentic Application in Power Grid Static Analysis: Automatic Code Generation and Error Correction Qinjuan Wang, Shan Yang, Yongli Zhu* Sun Yat-sen University, Guangzhou, China (wangqj35@mail2.sysu.edu.cn, yzhu16@alum.utk.edu) AbstractâThis paper introduces an LLM agent that automates power grid static analysis by converting natural language into MATPOWER scripts. The framework utilizes DeepSeek-OCR to build an enhanced vector database from MATPOWER manuals. To ensure reliability, it devises a three-tier error-correction system: a static pre-check, a dynamic feedback loop, and a semantic validator. Operating via the Model Context Protocol, the tool enables asynchronous execution and automatically debugging in MATLAB. Experimental results demonstrate that the system achieves a 82.38% accuracy regarding the code fidelity, effectively eliminating hallucinations even in complex analysis tasks. Keywordsâautomatic code generation, LLM agent, MCP, power grid static analysis, RAG I. INTRODUCTION Static analysis, such as power flow and Nâ 1 security assessment, is critical to the power gridâs stable operation. To perform such routines, users (e.g., system operators, planners, university researchers) have to use professional software/tools, such as MATPOWER [1], PSS/E, and so forth. Though most users are highly educated and experienced, coding every analysis task from scratch can be tedious and error-prone. Commercial-grade software, e.g, PSS/E and DIgSILENT, can support âScripting and Automationâ functionalities via a cus- tomized C++ or Python language subset. However, that still requires users to learn tautological programming manuals or grammar rules, which can be time-consuming. Moreover, debugging human-written code can be painful. As the mod- ern power grid becomes increasingly complex, the above- mentioned limitations make it difficult for users to handle multiple ad hoc system-analysis tasks in a timely manner. A potential solution to overcome the above challenge is nat- ural language processing (NLP)-based programming code gen- eration, i.e., directly mapping a userâs imperative instructions (in human language) to a script/code (in a target computer language). The emergence of Large Language Models (LLMs) and the related concept of agents or agentic workflows offer such possibilities. For example, in [2], the author proposes a method for infer- ring internal API information using RAG, which addresses the issue of inaccurate code completion by enabling LLMs to infer undocumented custom features. In [3], the author proposes CodeAct to integrate agent actions into a unified, executable Python space to solve complex problems through dynamic, multi-round interactions. In [4], the author adopts AnyTool, a hierarchical agent that uses a self-reflection mechanism to reactivate the agent when the initial solution proves unfeasible. In [5], the author proposes a knowledge filtering framework to eliminate noisy information and address the degradation in generation quality caused by irrelevant context. In [6], the author explores preliminary applications of LLMs in power system simulation, demonstrating the feasibility of LLM-based approaches for error feedback. This paper presents an end-to-end LLM-based tool that automates the power grid static analysis from human usersâ natural language commands to executable scripting code for the corresponding tool, viz., MATPOWER, in this paper. The merits are: 1) RAG-based automatic script (code) generation, 2) mechanisms for error-correction, and 3) integration of ad- vanced, industry-level toolchain(s) (e.g., DeepSeek-OCR [7]). From the userâs perspective, the proposed tool requires only a single natural language command to initiate the entire workflow; all subsequent code generation, execution, and iterative error correction are handled automatically by the agent. Internally, the agent employs a multi-turn feedback loop to resolve runtime errors and semantic inconsistencies. Finally, the proposed tool is validated on a series of sys- tem analysis tasks of varying complexity, ranging from easy to hard. Accuracy indices are also defined to quantitatively evaluate our toolâs performance. I. PROBLEM DESCRIPTION A. MATPOWER Introduction MATPOWER [1] is a toolbox designed for power grid static computation and optimization. The toolbox covers function- alities such as power flow (PF) (runpf, optimal power flow (OPF) (runopf ), results visualization, and so on. It requires a structural input called âcase fileâ to specify bus-, line-, and generator-parameters, as well as other meta information. B. Scenarios when LLM-Agent is Needed For example, LLM-Agent-based automatic code generation is useful in the following scenarios: 1) Online Contingency Analysis: The power system con- tingency analysis is essentially a customized power flow computation that considers contingencies involving specific components. When the considered contingencies change (e.g., switching from branch to bus contingencies), the old program must be modified promptly. 2) Operational Reliability Analysis: The power system re- liability analysis is essentially a Monte Carlo simulation com- bined with an (optimal) power flow calculation that considers combinations of component failures. When the considered arXiv:2604.09995v1 [eess.SY] 11 Apr 2026 Fig. 1: Overall architecture failure types vary (e.g., from generator failure to load failure, the old program must be modified promptly. There are many other tasks that may benefit from LLM- agent-based automatic code generation (cf. Section IV). ). The next section describes the architecture of our agent tool. I. AUTOMATIC CODE GENERATION AND ERROR CORRECTION A. Software Architecture: MCP, RAG, and LangChain 1) LangChain Framework: The system uses the LangChain framework [8] as its brain and wraps the DeepSeek [9] interface. LangChain manages a long context window through system prompts, few-shot examples, and usersâ conversation history to equip the LLM with a role, ensuring its output follows the grammar and other constraints of MATLAB and the MATPOWER toolbox. 2) Retrieval-Augmented Generation(RAG): To relieve the problems of the usual LLM, which lacks specific knowledge, such as power grid, and model hallucination, the system splits the userâs natural language instruction into multiple keywords through the query planner to analyze user intent before generating the code. It retrieves the relevant definition of API and its usage cases in the local knowledge base. This mechanism enhances the accuracy of code generation. 3) Model Context Protocol (MCP): The Model Context Protocol is a standardized communication protocol enabling AI agents to interact with external tools. Functioning similarly to a docking station, it extends their capabilities by providing access to diverse external resources(as shown in Fig. 2). The system implements the MCP standard [10] and packages the entire function into an MCP server, enabling the system to run independently and to be integrated with AI agents that support MCP as a tool. B. Construction of the Vector Database and RAG Mechanism 1) Data Preprocessing Based on DeepSeek-OCR: The MATLAB toolbox user manuals often include complex tables Fig. 2: Model Context Protocol (MCP) Introduction of parameters, mathematical formulas, code examples, and hundreds of pages. However, conventional PDF text extraction tools only extract unformatted character streams and break the semantic logic of documents, which makes it difficult for LLMs to understand the relationships between functions and their parameters. To solve this problem, the system introduced DeepSeek-OCR [7] for document preprocessing. DeepSeek- OCR is an optical character recognition model designed for complex documents. It delivers outstanding performance in layout analysis and long-form content, and it can convert user manuals to Markdown with high quality. Thus, the system processes the 265-page MATPOWER user manual [11] by running the DeepSeek-OCR model locally. The model precisely preserves the hierarchical headings and code blocks of the user manual, improving the accuracy of semantic segmentation during RAG retrieval. The system employs a fixed-size sliding-window algorithm to segment the markdown document, transforming it into fine-grained text chunks with independent semantics, laying the foundation for subsequent vectorization. 2) Enhanced Vector Indexing Construction and Retrieval Strategy: The system employs a pretrained transformer model to map the above text chunks to a high-dimensional semantic vector space, and constructs a vector indexing library using FAISS [12], a library for efficient similarity search and clus- tering of dense vectors. When constructing enhanced vector indexing, the system utilizes multiple source data, concatenating OCR-processed results (as shown in Fig. 3) with the original user manual text stream. This approach combines the strengths of both methods, ensuring the completeness of content and continuity of concepts while supplementing the row-column logic of complex tables and the syntactic structure of code blocks. Within the overall framework, the RAG mechanism does not match user requests directly; instead, it decomposes them into sub-requests and maps each sub-request to its keywords pre- cisely. Using these keywords, extract the top k most relevant fragments from the vector database. Then reassemble them and inject them into the LLM prompt as knowledge context to improve the usability of the generated code. C. Construction of the MCP/LangChain The following details the specific architectural implementa- tion of the LangChain-based agent workflow and MCP server. (a) Raw PDF tables(b) Identified authentic content(c) Structured Markdown output Fig. 3: Example of DeepSeek-OCR processing The module serves as the core controller of the system, respon- sible for coordinating the LLMâs cognition with interactions in the underlying executor environment. 1) MATPOWER Agent Workflow: After receiving the re- trieval results (as mentioned in Section I-B2), the agent proceeds to the prompt construction. Using the LangChain message management mechanism, it generates a composite system prompt that employs a three-layer structure. The top layer defines role parameters, specifying the coding language and constraints. The middle layer injects knowledge, e.g., the top-k retrieved manual fragments and standard MAT- POWER API conventions, while the bottom layer provides task instructions with user requests and few-shot examples. This structure ensures that the LLMâs reasoning is confined within the valid semantic space of the MATPOWER toolbox. Then, the agent launches a MATLAB engine session through the MATLAB Executor module. Executes generated code in MATLAB, monitors execution status, captures warning and error messages, and handles any exceptions that may occur. Specifically, the MATPOWER agent contains logic to drive the âgenerate-execute-correctâ loop. When code execution fails in MATLAB, the agent does not report an error and exit immediately. Instead, it automatically triggers an error correction mode. The agent iterates this loop continuously until either the maximum threshold is reached or semantic validation succeeds. Fig. 4: MCP architecture 2) MCP Architecture and Inter-Process Communication: To overcome the closed nature of conventional scripting tools, the system implements an MCP server that enables it to be invoked as a tool by the external ecosystem (Fig. 4). Since MATLAB simulations are computationally intensive, the MCP server does not use a simple synchronous approach. Instead, it uses an asynchronous architecture that combines asyncio and subprocess. This allows the MCP server to avoid blocking other simulation tasks while waiting for a simulation task to complete. When receiving a request, the server process forks an independent Python subprocess to run the MAT- POWER agent, preventing the entire service from becoming unavailable if a single simulation hangs. To address data exchange between the two processes, the system designs an application-layer protocol. After completing its task, the subprocess packages the final code, execution status, and debugging logs into a standard JSON data packet and sends it via the standard output stream. To precisely capture target data from a stream mixed with MATLAB logs, the subprocess appends an identifier to the JSON packet header. The MCP server monitors the subprocessâs output, captures and parses the subsequent JSON content immediately when recognizing the identifier, converts it into a format compliant with the MCP standard, and responds to the client. D. Mechanism of Error Feedback and Correction To address hallucinations and logical errors in code gener- ated by LLMs, the system employs a three-tier architecture comprising a static pre-check, a dynamic feedback loop, and a semantic validator. 1) Static Pre-check: Before MATLAB execution, the static pre-check module performs a scan based on MATPOWER conventions, as shown in Fig. 5. It utilizes fuzzy matching to rectify typos in option names and automatically injects the def ine constants; statement if specific constants (e.g. P D or GEN BU S) are detected. This refinement filters out elementary errors, minimizing the overhead of the iterative feedback loop. 2) Feedback Loop: As shown in Fig. 1, when MATLAB returns a runtime error, the agent starts the feedback loop. The system captures the error stack message and combines it with static pre-check hints to form an error report. This report also includes the failed code from the previous iteration, the user request, and related context, presented in RAG format, to construct new prompt feedback for the LLM. Then LLM analyzes and rewrites the code accordingly. This process sets a maximum iteration threshold to prevent falling into an endless loop. 3) Semantic Validator: To address cases where the gen- erated code runs successfully but fails to meet the userâs needs, the system adds a semantic sanity-check module after Fig. 5: Static Pre-check Fig. 6: Output visualization the MATLAB executor. An independent LLM-based semantic validator compares the user request against the final code. If logical inconsistencies are detected, they are considered semantic errors. Considering potential contradictions in the user request, the assessment results are quantified as âCriticalâ or âMinorâ levels, ⢠Critical-level semantic deviations are forcibly deemed failures, triggering a new round of the feedback loop. ⢠Minor-level semantic deviations are output as warning messages, ensuring generated scripts pass the check de- spite flaws in user requests. E. GUI and Output Visualization To enhance the interactive experience, this paper also develops a web visualization using the Chainlit framework [13]. Users can submit requests in natural language, and the interface provides real-time feedback on the systemâs thinking process (as shown in Fig. 6). IV. CASE STUDY To assess the performance of the proposed MATPOWER agent, basic and ablation experiments are conducted, focusing on retrieval strategies, the feedback loop, the query planner, and the semantic validator. A. Case Settings The basic experiment sets up the following four retrieval modes, ⢠no RAG ⢠Mode 1 (OCR-Markdown) Perform keywords matching directly on the OCR-processed Markdown without con- structing a vector database, ⢠Mode 2 (PDF Vector DB) Construct a vector database using the original PDF manual only, extracting original text fragments, ⢠Mode 3 (Enhanced Vector DB, âRAG+â) Merge PDF manual with OCR-processed Markdown to construct an enhanced vector database, combining semantic retrieval with structured information. To investigate the effectiveness of each component in the proposed framework, the ablation experiment settings are shown in Table I. TABLE I: Ablation Experiment: System Component Config- urations Ablation Configuration RAG Mode 3 RAG+Feedback PlannerValidator Full Modelâ Single Passâ Simple Searchâ Execution Onlyâ These experiments test 10 simulation tasks, with a represen- tative subset of six tasks detailed in Table I. These scenarios span a wide range of complexity, from standard power flow ex- ecutions to sophisticated iterative algorithms and multi-matrix synchronization. Each task is executed in isolation to ensure results are free of interference from historical conversation context. To quantitatively evaluate the agentâs performance, we de- fine the Code Generation Fidelity (CSGF) index. This metric considers both the semantic and the efficiency of the feedback loop. CSGF i = S i Ă N threshold â (n i â 1) N threshold (1) where S i is the semantic score for task i (1.0 for perfect implementation, 0.8 for necessary technical workarounds, and TABLE I: Representative Task Benchmarks Task Type Task ID Natural Language Request Easy Task 1Load case14. Increase the active load at bus 2 by 15%. Run a DC power flow and display the results. Task 2Load case57. Run a standard AC power flow and a DC power flow. Compare the resulting voltage magnitudes (Vm) and active power branch flows (Pf) between the two methods to evaluate the accuracy of the DC linearization. Task 3Load case39. Compare the convergence performance of the standard Newton-Raphson algorithm and the Fast Decoupled (XB) algorithm. Set the maximum number of iterations to 10 for both and report whether each algorithm successfully converges. Hard Task 1Load case39. Calculate the Total Transfer Capability (TTC) from Area 1 to Area 2. Write a MATLAB loop that iteratively increases Area 2 loads and Area 1 generation by 5% increments. In each step, run a DC OPF and check if any branch flow exceeds its RATE A limit. Stop the iteration at the last feasible point before a violation and print the maximum successfully transferred power in MW. Task 2Load case9. Add a new generator at Bus 4 (a load bus). You must change Bus 4 type to PV in mpc.bus, add the generator row to mpc.gen, and add a cost row to mpc.gencost. Ensure the BUSTYPE and generator location are perfectly synchronized before running OPF. Task 3Run AC OPF on case30. Implement a user-defined linear constraint Pg1 + Pg2⤠30 MW using mpc.A, mpc.l, and mpc.u. Manually calculate the column in- dices for Pg1 and Pg2 based on the variable ordering without using helper functions. 0 for final critical logic errors or final failure), n i is the number of iterations required to reach success, and N threshold = 5 is the maximum allowed iterations. To assess the overall stability and reliability across the entire benchmark, the Global CSGF Accuracy (GCA) is calculated for each configuration as follows. GCA =  ďŁ 1â v u u t 1 K K X i=1 (1â CSGF i ) 2   Ă 100%(2) where K represents the total number of tasks. B. Experiment Results and Analysis The performance of the MATPOWER agent is comprehen- sively evaluated across diverse configurations using the CSGF and GCA metrics. The GCA for all configurations is illustrated in Fig. 7. The results show that the Full Model (Mode 3) is the most robust configuration, achieving a GCA of 82.38%, which significantly outperforms all other retrieval modes and ablation variants. Mode 2 utilizes traditional PDF chunking and achieves a GCA of 64.65%. By integrating structured OCR-Markdown data, Mode 3 improved the generation fidelity by approxi- mately 17.7%. This validates that linearized, structured tech- nical documentation provides a superior knowledge foundation for LLM-based agents compared to fragmented PDFs. Mode 3Mode 1Mode 2 Simple Search Single Pass no RAG Execution Only 0 20 40 60 80 100 Global CSGF Accuracy (GCA) % 82.38% 25.82% 64.65% 68.38% 67.75% 64.36% 51.42% Fig. 7: Overall system accuracy (GCA) across different con- figurations. To investigate the agentâs behavior under different logical loads, tasks are categorized into Easy and Hard groups. The mean CSGF scores per group are shown in Fig. 8. Mode 1 achieves a perfect score of 1.0 on easy tasks but plummets to 0.16 on hard tasks. This result indicates that while the OCR-processed Markdown document can meet simple requests, it fails to generate compliant code when handling complex logical operations, such as cross-matrix synchronous modifications (Hard Task 2) or manual variable index cal- culations (Hard Task 3), due to the absence of semantic associations. Mode 2 underperforms on easy tasks compared to the Zero-shot, which is attributed to retrieval noise and context fragmentation inherent in raw PDF vectorization. Fragmented chunks may truncate simple API definitions or introduce irrelevant background information, distracting the LLM from straightforward instructions. Fig. 8: CSGF performance grouped by task complexity. The ablation of the semantic validator (Execution Only) exhibits a deceptively high execution success rate, a manual audit of its logic and subsequent recalibration of the semantic score S i reveal a GCA collapse to 51.42%. This confirms the agentâs reliability. Without the validator, the agent produces about 31% hallucinated code that is syntactically correct and executable in MATLAB but violates the userâs intent. V. CONCLUSION This paper addresses the challenges of laborious parameter configuration and debugging efforts in complex power system static analysis tasks by proposing an LLM-based automatic script generation framework that leverages enhanced retrieval and error correction. The experiment results demonstrate that the proposed framework significantly reduces hallucinations on hard tasks and the average number of iterations required for successful code execution. The error-correction mechanism, combining static pre-check and dynamic feedback, enables the system to automatically repair errors in a finite number of iterations, even when the initially generated code contains errors. Although enhanced retrieval incurs slight delays in normal tasks, its advantage of avoiding multi-turn human-AI dialogues in complex tasks makes it more efficient than coding from scratch by humans. Future work will explore integrating our approach with other power system-related toolboxes. REFERENCES [1] R. D. Zimmerman, C. E. Murillo-Sanchez, and R. J. Thomas, âMAT- POWER: Steady-State Operations, Planning and Analysis Tools for Power Systems Research and Education,â IEEE Transactions on Power Systems, vol. 26, no. 1, p. 12â19, Feb. 2011. [2] L. Deng, X. Ren, C. Ni, M. Liang, D. Lo, and Z. Liu, âEnhancing Project-Specific Code Completion by Inferring Internal API Informa- tion,â IEEE Transactions on Software Engineering, vol. 51, no. 9, p. 2566â2582, Sept. 2025, doi: 10.1109/TSE.2025.3592823. [3] X. Wang, Y. Chen, L. Yuan, Y. Zhang, Y. Li, H. Peng, and H. Ji, âExecutable code actions elicit better LLM agents,â in Proceedings of the 41st International Conference on Machine Learning (ICMLâ24), Vienna, Austria, 2024, Article no. 2054. [4] Y. Du, F. Wei, and H. Zhang, âAnyTool: self-reflective, hierarchical agents for large-scale API calls,â in Proceedings of the 41st International Conference on Machine Learning (ICMLâ24), Vienna, Austria, 2024, Article no. 470. [5] M. Qiang, Z. Wang, S. Li, and G. Zhou, âExploring Knowledge Filtering for Retrieval-Augmented Question Answering,â IEEE Transactions on Audio, Speech and Language Processing, vol. 34, p. 1049â1060, 2026, doi: 10.1109/TASLPRO.2026.3658957. [6] M. Jia, Z. Cui and G. Hug, âEnabling Large Language Mod- els to Perform Power System Simulations with Previously Unseen Tools: A Case of Daline,â CoRR, vol. abs/2406.17215, 2024, doi: 10.48550/ARXIV.2406.17215. [7] H. Wei, Y. Sun and Y. Li, âDeepSeek-OCR: Contexts Optical Com- pression,â arXiv preprint arXiv:2501.18234, 2025. [Online]. Available: https://arxiv.org/abs/2501.18234 [8] LangChainOpen-sourceFramework.[Online].Available: https://github.com/langchain-ai/langchain [9] DeepSeek-AI, âDeepSeek LLM: Scaling Open-Source Language Models with Longtermism,â arXiv preprint arXiv:2401.02954, 2024. [Online]. Available: https://github.com/deepseek-ai/DeepSeek-LLM [10] ModelContextProtocol.[Online].Available: https://modelcontextprotocol.io [11] R. D. Zimmerman and C. E. Murillo-Sanchez, MATPOWER UserâsManual,Version8.1,2025.[Online].Available: https://matpower.org/docs/MATPOWER-manual-8.1.pdf [12] Faiss: A library for efficient similarity search and clustering of dense vectors. [Online]. Available: https://github.com/facebookresearch/faiss [13] Chainlit: Build Python LLM apps in minutes. [Online]. Available: https://github.com/Chainlit/chainlit