Code Interpreter for Business Central

Introduction

When people first start experimenting with Large Language Models (LLMs), there’s one thing almost everyone immediately tries: asking the LLM questions about their own data. The typical first attempt looks like this—take all your data, sent it into the LLM, and start asking questions, hoping for quick insights. But very soon, reality hits hard:

  • LLMs can’t handle massive data dumps. Every model has its limits, and the amount of data it can process in one go usually is just not enough.

  • LLMs aren’t math or data experts. Sure, models like GPT-o1 or GPT-o3 pro are incredibly powerful. But fundamentally, they’re designed to understand language—not crunch big datasets or perform complex math instantly. Plus, they’re expensive and can slow down drastically with big inputs.

At the end of the day: simply feeding tons of data into an LLM and hoping for magic insights just doesn’t work.

Yet, the demand for quick and intuitive data insights is huge. Everyone in your business—from sales reps to CFOs—wants answers from their data, and they want them fast.

Surprise, surprise – here comes Microsoft Dynamics 365 Business Central 😀 We already have structured business data. We have super-smart LLMs ready to help. We just need to connect these two somehow.

Today, I’m excited to introduce you to the Code Interpreter for Business Central, totally free and open-source! 

Solution was inspired by a talk I gave about five years ago on the Areopa channel. Back then, I explained why Python is great for analyzing large datasets compared to AL, which is slower and struggles with complex data tasks.

Python became super popular because it’s fast, flexible, and powerful—perfect for machine learning and big data. When you combine Python with LLMs, amazing things happen:

  • Ask questions in plain English (or any language!).

  • Let the LLM figure out exactly what data you need.

  • Auto-generate Python code to fetch and analyze this data.

  • Run this code quickly and securely.

  • Pass the results back to the LLM to create clear, natural language answers.

This blog will show you how I brought this cool approach directly into Business Central, letting anyone turn business questions into data insights—instantly.

Let’s dive in!

1. How It Works

Here’s a simple breakdown of how the Code Interpreter for Business Central turns your questions into data insights

1. You Ask the Question

Just type your business question right inside Microsoft Dynamics 365 Business Central, using simple language. For example:

  • “What were my top 5 customers last quarter?”

  • “Analyze the trend of overdue invoices over the past 6 months”

  • “Create a pie chart showing sales by region.”

2. AI Generates Python Code

Your question goes to Azure OpenAI, which instantly writes Python code to fetch and process exactly the data you need.

3. Secure Execution

This Python code runs safely inside an Azure Function—think of it like a secure sandbox, making sure your data stays safe.

4. Data Fetching

The Azure Function uses a secure OAuth connection to grab the exact data needed from Business Central via its API.

5. Data Analysis & Visualization

Powerful Python tools like pandas, statsmodels, scikit-learn, and matplotlib crunch the data and quickly create any visuals you need.

6. Intelligent Error Handling

If something doesn’t go perfectly at first, don’t worry! There’s a built-in self-correcting “agent” (now you should be excited 🤭😅) that automatically spots issues, double-checks the data, and tries again with smarter Python code (up to three attempts).

7. Natural Language Answer Generation

Once analysis is finished, the results go back to Azure OpenAI, which transforms them into a clear, easy-to-understand answer.

8. Insightful Presentation

You get clear insights directly in Business Central, along with helpful visuals if needed. Making data-driven decisions has never been faster or simpler. Plus, you can always go deeper and explore the “thinking process” behind the answers.

Now, getting data insights is just one question away!

2. User Experience Examples

To see the magic of Code Interpreter for Business Central in action, here are some real-life questions it can easily handle:

Top 5 Customers in Sales

“Who were my top 5 customers in terms of sales last quarter?”

Inventory Analysis and Visualization

“Show me inventory items with the highest turnover rate as a chart.”

Check out these short videos showing exactly how Code Interpreter for Business Central answers these questions!

3. The Tech Behind the Magic

Let’s dive under the hood at how this magic happens. There are three key ingredients:

  • Business Central Extension (Copilot UI built right into BC)

  • Azure Functions (Safe Python sandbox)

  • Azure OpenAI (GPT-4o for Python coding & summarizing)

Business Central Extension (Copilot UI integrated into BC)

I built the Business Central extension using Microsoft’s powerful Copilot Toolkit, a modern way to easily add smart, AI-driven experiences directly inside Business Central.

The extension performs three major steps: python code generation, execution and finally answering to the user. 

1. Python Code Generation

When you ask a question, the system first generates Python code tailored specifically for fetching and analyzing your data:

				
					PythonCode := PythonGenerator.GenerateCode(InputText, AzureOpenAI);
				
			

To help the AI generate accurate Python code, we provide detailed instructions including:

  • Identifying required data from Business Central APIs.

  • Using a provided function get_bc_data(relative_url, environment) to securely fetch data.

  • Analyzing data with Python libraries like pandas, numpy, matplotlib, scikit-learn, and statsmodels.

  • Strictly following security rules (no file access or dynamic code execution).

				
					You are a Python code generator designed to help analyze data from Microsoft Dynamics 365 Business Central.

The user will ask a business-related data question.

### Guidelines

Your task is to:
1. Generate valid Python code to retrieve and analyze Business Central data.
...
				
			

The full system prompt is available here

Critically, we provide essential context:

  • The current Business Central environment (sandbox, production).

  • The specific company ID.

  • A dynamically generated list of available APIs (standard and custom), ensuring accurate and valid API usage.

				
					        // Create environment information object
        EnvInfoObj.Add('companyId', GetCompanyId());
        EnvInfoObj.Add('environment', EnvironmentInformation.GetEnvironmentName());
        JsonObj.Add('environmentInformation', EnvInfoObj);

        // Add available APIs
        AvailableEntitiesArray := GetAvailableAPIsAsJson();
        JsonObj.Add('availableEntities', AvailableEntitiesArray);

        // Add current date
        JsonObj.Add('currentDate', Format(Today(), 0, '<Year4>-<Month>-<Day>'));

        // Add user question
        JsonObj.Add('userQuestion', Question);

        // Convert to yaml
        JsonObj.WriteToYaml(YamlText);
        exit(YamlText);
				
			

We package all this helpful context in a YAML format (converted from JSON using Business Central’s 26.0 new AL function), optimizing token usage and keeping interactions with Azure OpenAI quick and cost-effective.

If the data needed isn’t available in a standard API, you can easily publish it as a custom API. The Code Interpreter will immediately recognize and use it!

2. Python Code Execution

Once the Python code is ready, it runs securely in an Azure Function. This isolated sandbox safely executes the code, retrieving and analyzing the required data. The only requirement is that the necessary data must be accessible via the Business Central API.

				
					    [TryFunction]
    procedure TryExecuteCode(PythonCode: Text; var Result: Text)
    begin
        ClearLastError();
        Result := ExecuteCode(PythonCode);
    end;
				
			

Results come back in a structured format:

				
					output = {
    "data": result,  # Can be a dictionary, list, number, or string
    "chart_images": chart_images  # Base64-encoded images
}
				
			

The full execution AL code sits here.

3. Final Answer Preparation

After execution, analyzed data goes back to Azure OpenAI again to create a simple, easy-to-read summary:

				
					FinalAnswer := GenerateSummary(InputText, ExecutionResult, AzureOpenAI);
				
			

Azure OpenAI gets your original question and Python results, then creates a short, friendly answer that clearly shows key insights (without confusing technical jargon).

				
					You are an assistant that summarizes data for a Business Central user in a clear and helpful way.

Your task:
1. Understand what the user is asking.
2. Interpret the result correctly.
3. Write a short, human-friendly summary that clearly answers the question.
...
				
			

The full prompt you can find here.

Finally, results beautifully display inside Business Central using simple HTML, complete with charts or visuals:

				
					        ResultInHtml := UIHelper.GenerateResultInHtml(FinalAnswer, GetChartImages(ExecutionResult));
				
			

Result in a HTML format is presented in the RichContent type control of the Prompt dialog page.

So, no matter how complex the data analysis, you always get clear, simple answers!

Azure Functions: Secure Python Sandbox

Azure Functions serve as our secure sandbox—an isolated, cloud-based environment—where automatically generated Python code runs safely, processing your Business Central data securely within your Azure subscription.

  • Receive Python Code: Azure Function gets the generated Python code via secure HTTP.

  • Secure Execution: Code runs safely with strict rules (no risky file operations or system access).

  • Fetch & Analyze Data: Python grabs data securely using OAuth from Business Central APIs and analyzes it using powerful libraries.

  • Structured Results: Results return as structured JSON and base64-encoded images ready for Business Central visualization.

				
					@app.route(route="execute", methods=["POST"])
def main(req: func.HttpRequest) -> func.HttpResponse:

    try:
        body = req.get_json()
        user_code = body.get("code")

        # Improved security check using regex word boundaries
       ...
        # Prepare safe execution environment
       ...
       # Execute generated code
        exec(user_code, safe_globals, safe_locals)
        
        output = safe_locals.get("output")
       ...
        return func.HttpResponse(
            json.dumps(output),
            status_code=200,
            mimetype="application/json"
        )

				
			

Deployment and Setup

Getting started is easy:

  1. Deploy the repo via Visual Studio Code (guide here).

  2. Set up Business Central API access.

  3. Configure Azure Function with environment variables (Tenant ID, Client ID, Client Secret).

Security and Safeguards

Everything runs securely—only safe Python libraries are allowed, and risky operations are blocked.

With Azure Functions, you can confidently run complex Python data analyses securely, quickly, and efficiently, keeping your data safe and without need of sending to LLM.

4. Smart Error Handling – Self-Improving AI Agent 🔥

Errors happen—but with intelligent retry mechanism, they’re quickly identified, analyzed, and corrected automatically. This step features a powerful, autonomous, self-correcting agent.

Automatic Error Analysis and Correction

When an error occurs during Python code execution, the system doesn’t just give up. Instead, it autonomously:

  • Analyzes the Error: The system evaluates what caused the failure, whether it was an incorrect API endpoint, a missing field, or some other issue.

  • Self-Decides Next Steps: Depending on the nature of the error, the AI autonomously decides how best to fix it. For example:

    • If the wrong API was used, it dynamically explores available APIs to find the correct endpoint.

    • If there’s a field mismatch, it retrieves just a single record ($top=1) to inspect the exact data structure.

  • Generates Diagnostic Python Code: The agent generates and runs diagnostic Python code to pinpoint exactly what’s needed to resolve the issue.

				
					        PythonCode := GenerateErrorAnalysisCode(OriginalQuestion, FailedCode, ErrorMessage, AzureOpenAI);
        if not PythonExecutor.TryExecuteCode(PythonCode, Result) then
            Error('Failed to execute the error analysis code. Error: ' + GetLastErrorText());

				
			

Self Improving AI Agent uses the next system prompt

				
					    local procedure GetErrorAnalysisPrompt(): Text
    begin
        exit(@'
You are an assistant responsible for helping debug failed Python scripts for Business Central data analysis.

You will receive:
1. The original user question.
2. The previously generated Python code that failed to execute.
3. The error message that was returned.

Your task is:
- Analyze why the code might have failed.
- Generate new Python code that will help you understand the structure of the Business Central API response.
- This may include inspecting field names, printing the first row, or listing available keys or types.
...


				
			

It’s critical for the user to understand the ongoing process, so the retry logic is nicely handled in the UI.

Self-Improving Through Retries

The system tries this self-correction process up to three times, learning from each try. Every attempt:

  • Makes the system smarter about data structures and APIs.

  • Improves the Python code for better accuracy.

  • Ensures quick, minimal data requests.

This way, your questions get answered reliably and fast, turning mistakes into self-learning process!

The “Thinking Process”

Ever wonder how the magic happens? You can always peek behind the scenes and see the exact Python code that led to your answers.

5. Security and Data Privacy

Data security always comes first:

  • Your Data Stays Yours: Everything runs securely inside your Azure subscription.

  • Secure API Access: Uses safe OAuth connections with strictly limited permissions.

  • Sandboxed Execution: Python scripts run securely in Azure Functions—keeping data completely safe.

6. How to Get Started

Getting started is quick and easy:

  • Prepare Azure: Set up Azure OpenAI and Azure Function App (guide here).

  • Install the Extension: Deploy the AL extension to your Business Central (guide here).

  • Activate and Set Up Copilot: Enable Code Interpreter Copilot in Business Central and start asking your data questions right away!

Check out the whole workflow here

7. Customization and Expansion

Want more? You can:

  • Publish your own APIs to access additional Business Central data. No need to change anything, Code Interpreter will discover new APIs automatically.

  • Tailor prompts and visuals to fit your unique business needs.

  • Expand analysis capabilities by adding more Python packages in the prompt and Azure Function.

Final Thoughts

Honestly, I believe this is the best practical solution at the moment for doing real, meaningful data analysis in Business Central using LLMs. Sure, it’d be amazing to have direct database access, dynamically create and run query objects, generate and execute AL code instantly, or—my personal dream—run Python directly on Business Central data with full platform support. But we’re not there yet… 🤞

Until then, here it is: Code Interpreter for Business Central, fully open-source and ready for you to use.

Check out the GitHub repository, give it a try, and please share your thoughts!

Feel free to contribute, collaborate, or just reach out—let’s make Business Central smarter together!

Share Post:

Leave a Reply

About Me

DMITRY KATSON

A Microsoft MVP, Business Central architect and a project manager, blogger and a speaker, husband and a twice a father. With more than 15 years in business, I went from developer to company owner. Having a great team, still love to put my hands on code and create AI powered Business Central Apps that just works.

Follow Me

Recent Posts

Tags