Skip to main content

Expense Tracking

Tasks Using External APIs

Tasks that interact with third-party or external APIs must include additional metadata, ProcessedUnit, to ensure their usage and cost can be measured and attributed correctly.

Ensure that ProcessedUnit accurately reflects all the units the vendor uses to calculate charges for the task.

Syntax

flow.meta().addProcessedUnit(ProcessedUnit.from('vendor', 'operation', 'model', getCount(result)));

Example

@Executor(Commands.OPENAI_IMAGE_EDITING)
export class OpenaiImageEditingExecutor implements TaskExecutor {

async processTask(
taskCommand: OpenAiImageEditingCommand,
meta: TaskMetadata,
flow: Flow,
): Promise<OpenAiImageEditingResult> {
const result = await this.openaiImageEditingService.editImage(taskCommand as OpenAiImageEditingCommand, flow);

flow.meta().addProcessedUnit(ProcessedUnit.from('openai', 'input_text_tokens', taskCommand.model, result.usage?.input_tokens_details.text_tokens / 1e6));
flow.meta().addProcessedUnit(ProcessedUnit.from('openai', 'input_image_tokens', taskCommand.model, result.usage?.input_tokens_details.image_tokens / 1e6));
flow.meta().addProcessedUnit(ProcessedUnit.from('openai', 'output_image_tokens', taskCommand.model, result.usage?.output_tokens / 1e6));

return { result: result.editedImageResult };
}
}

note

Ensure your project uses version 4.3.0 or higher of @picsart/pa-pluggable-workers-core.