Lets say you build a ‘Code App’ and you need to reason over the user’s input with the help of AI. For example, read a customer ticket and decide its category and priority.

An AI Builder prompt is a good fit for this. You write the instruction once in plain English, and the prompt returns a structured answer each time you call it.

In this article, lets see how to call an AI Builder prompt from a Power Apps ‘Code App’. We call the same prompt in two ways:

  • Using a cloud flow. The ‘Code App’ calls the flow through a service that the ‘pa’ CLI generates.
  • Using a direct call from the ‘Code App’, with no flow in between.

As a bonus, we also build a page in the ‘Code App’ that shows the AI credits consumed by each call, along with the prompt name and the model used.

Why call an AI prompt from a Code App?

Our scenario is a support desk ‘Ticket Triage’ app. We paste a customer ticket, and the prompt returns a category, a priority, a summary and a suggested reply. The ‘Code App’ shows the result and tracks the cost of each call.

Prerequisites

  • A Power Platform environment with Dataverse and AI Builder prompts enabled.
  • Code apps turned on for the environment.
  • Visual Studio Code.
  • A solution to hold the prompt and the cloud flow.

Create the AI prompt

  • Sign in to the Power Apps maker portal and select your environment.
  • Go to ‘AI hub’ > ‘Prompts’ > ‘Build your own prompt’.
  • Select the generated name at the top left and rename the prompt to ‘Ticket Triage’.
  • Add the following instruction.
You are a support desk assistant. Read the customer support ticket below and triage it.
Return:
- category: Billing, Technical, Account or Other
- priority: Low, Medium or High
- summary: max 20 words
- suggestedReply: 2-3 sentences
Don't include JSON markdown in your answer.
Ticket: [TicketText]
  • Add a text input named ‘TicketText’ and provide a sample ticket in ‘Sample data’.
  • Click ‘Test’. The prompt returns the category, priority, summary and suggested reply.
  • Click ‘Save’.

Now we have the prompt ready. Next we will add the prompt to a solution and create the cloud flow.

Add the prompt to a solution and create the cloud flow

  • Open your solution and click ‘Add existing’ > ‘AI Model’.
  • Select the ‘Ticket Triage’ prompt. It shows up in the solution with the type ‘AI Model’.
  • From the same solution, create a new instant cloud flow.
  • Name the flow ‘Ticket Triage Flow’ and select the ‘When Power Apps calls a flow (V2)’ trigger.
  • Add a text input named ‘TicketText’ to the trigger.
  • Add the ‘Run a prompt’ action. Select the ‘Ticket Triage’ prompt and map ‘TicketText’ to the trigger input.
  • Add the ‘Respond to a Power App or flow’ action. Return four text outputs: ‘category’, ‘priority’, ‘summary’ and ‘suggestedReply’.
  • Save the flow and copy the flow ID from the browser URL.

Note: A ‘Code App’ can only call solution-aware flows. That is why we created the flow inside the solution.

Now we have the cloud flow created. Before moving to the ‘Code App’, lets test the flow.

Test the cloud flow

  • Click ‘Test’, enter a sample ticket and click ‘Run flow’.
  • The flow runs and the ‘Respond to a Power App or flow’ action returns the prompt output.

Now we have a working cloud flow. Lets build the ‘Code App’ and call this flow.

Create the Code App and call the cloud flow

The full source code is available in my GitHub repo. Clone it and connect it to your environment using the Power Apps CLI (‘pa’ command).

git clone https://github.com/RajeevPentyala/TicketTriageCodeAppAIModelDemo.git
cd TicketTriageCodeAppAIModelDemo
npm install
npx pa app init
npx pa app add flow --flow-id <your-flow-id>
  • ‘pa app init’ signs you in, asks for the environment and creates the ‘power.config.json’ file.
  • ‘pa app add flow’ generates the ‘TicketTriageFlowService’ class under ‘src/generated’.

The ‘Triage’ tab calls the flow with a single line of code.

const response = await TicketTriageFlowService.Run({ text: ticketText.trim() })

Note: The generated input property is ‘text’, which is the internal name of the trigger input. ‘TicketText’ is its display name.

  • Build the app and publish it to the environment.
npm run build
npx pa app push
  • Open the app URL that ‘pa app push’ prints. Paste a ticket and click ‘Triage’.
  • The app shows the category and priority as badges, along with the summary and the suggested reply.

Now we have the ‘Code App’ calling the prompt through the flow. Next lets call the same prompt directly from the ‘Code App’, without a flow.

Call the prompt directly from the Code App

For the direct call, we use the Dataverse ‘Predict’ action. It runs an AI model by its ID. A ‘Code App’ can add Dataverse actions using the ‘pa app add dataverse-api’ command.

npx pa app add dataverse-api --api-name Predict
  • This generates the ‘PredictService’ class.
  • Copy ‘.env.example’ to ‘.env.local’ and set ‘VITE_TICKET_TRIAGE_MODEL_ID’ to the prompt ID (‘msdyn_aimodelid’).

The ‘Direct Predict’ tab calls the prompt as below.

const response = await PredictService.Predict(MODEL_ID, '2.0', undefined, {
'@odata.type': '#Microsoft.Dynamics.CRM.expando',
TicketText: ticketText.trim(),
'TicketText@odata.type': 'String',
}, JSON.stringify({ consumptionSource: 'PowerApps', partnerSource: 'AIBuilder' }))
  • The last parameter is the ‘source’. It tells AI Builder that the call came from ‘PowerApps’.
  • The prompt output is returned in ‘responsev2.predictionOutput’.

Build and push the app again. Open the ‘Direct Predict’ tab, paste a ticket and click ‘Triage (direct)’.

Now we have covered both ways to call the prompt. As a bonus, lets build a page that shows how many AI credits each call consumed.

Bonus: Track AI credits in the Code App

Each prompt call writes a row to the ‘msdyn_aievent‘ Dataverse table. The ‘msdyn_eventdata‘ column holds the consumption details as JSON.

{
"messageConsumption": { "featureName": "Text and generative AI tools (basic)", "units": 2, "consumption": 0.2 },
"consumptionSource": "PowerAutomate",
"partnerSource": "AIBuilder",
"llmModelName": "gpt-41-mini-2025-04-14"
}

Here is what each field tells us:

  • ‘consumption’ is the credits consumed by the call.
  • ‘consumptionSource’ is where the call came from, for example ‘PowerAutomate’ or ‘PowerApps’.
  • ‘partnerSource’ is the product, for example ‘AIBuilder’ or ‘MicrosoftCopilotStudio’.
  • ‘llmModelName’ is the LLM model that ran the prompt.

Lets add the table as a data source.

npx pa app add data-source --connector dataverse --table msdyn_aievent

The ‘AI usage’ tab reads the latest events using the generated ‘Msdyn_aieventsService’.

const response = await Msdyn_aieventsService.getAll({
select: ['msdyn_aieventid', 'createdon', 'msdyn_eventdata', 'msdyn_quicktest', '_msdyn_aimodelid_value'],
filter: hideTests ? 'msdyn_quicktest eq false' : undefined,
orderBy: ['createdon desc'],
top: MAX_ROWS,
})
  • The two calls through the flow show ‘PowerAutomate’ under ‘Used in’, with 0.2 credits each.
  • The direct call shows ‘PowerApps’ under ‘Used in’, because no flow was involved.

How the credits work?

  • Credits are not consumed from individual users. Every call from the ‘Code App’ consumes credits at the environment level.
  • The environment uses AI Builder credits first. When none are available, it consumes Copilot Credits from prepaid packs or pay-as-you-go.
  • Users still need a license to run the ‘Code App’, such as Power Apps Premium. This license is separate from credits.

Note: Testing a prompt in the prompt builder is free.

Summary

  • A cloud flow is a good fit when the flow does more work, such as saving the result or sending an email.
  • The direct call skips the flow and keeps the call inside the ‘Code App’.
  • As a bonus, the ‘msdyn_aievent’ table shows the AI credits each call consumed and where the call came from.

The full source code is available in the GitHub repo.

🙂

Leave a Reply