Prompt Template Batch Processing in Apex

To asynchronously generate large quantities of responses for prompt templates you can use Prompt Template Batch Processing. You can batch and execute jobs in Flow and Apex. For Flow, see Flow Core Action: Prompt Template Actions.

See the object reference guide for AiJobRun and AiJobRunItem.

There are several immutability and status constraints for batch processing standard objects.

Once an AiJobRun begins processing and the status updates to InProgress, some AiJobRunItem fields become immutable.

  • You can't change the AiJobRunId and Input field values.
  • AiJobRunItem schema fields set to a non-null value (except status) can't be changed.
  • You can't delete the AiJobRun or any of its AiJobRunItems.

These statuses for both AiJobRun and AiJobRunItem can't be changed by the user:

  • InProgress
  • Completed
  • Failed

To initiate an AiJobRun for batch execution, update the status to ReadyToStart.

There's a limit of 1,000 AiJobRunItems for each AiJobRun.

This example demonstrates how to use Apex to schedule and run batch jobs.

  1. Create an AiJobRun object.
  1. Create an AiJobRunItem list with information about each job.

Input parameter names must include the Input: prefix. If your prompt template expects a record input, pass a JSON payload with the record id nested under the input name.

  1. Update the AiJobRun Status to trigger execution.

You can use Apex triggers to automatically respond when a batch job completes. By creating a trigger on the AiJobRun object for update operations, you can detect when the Status field changes to Completed.

When the job completes, you can query the related AiJobRunItem records to access both the original input data and the generated responses. The Input field contains the input data for each individual item within the job run, and the Response field contains the corresponding LLM response.

For more information about Apex triggers, see the Apex Developer Guide.

AiJobRun.JobType can differ based on how the batch is initiated.

  • Apex-created batch runs: PromptTemplate
  • Flow (Prompt Template Batch Generation action): GeneratePromptAsyncIA