SAP Home Learn Build Integrate Model Operate Extend with AI ConnectTutorial navigator Knowledge Graph API Devtoberfest Developer Advocates App Space

Manage my Account SAP Devs YouTube ↗ Learnings ↗ Community ↗ Provide Feedback ↗
Logout
⤢ Open full site

Optimize Prompts for Tool Calling Using a Custom LLM-as-a-Judge Metric in SAP AI Core

Use SAP AI Core Prompt Optimization to automatically rewrite a weak base prompt into a strict tool-calling prompt — this time creating your own custom `tool-call-accuracy` evaluation metric from code, running the full optimization pipeline, and comparing the result against the built-in `JSON_Match` metric.

Overview

🎓 beginner 60 min. SAP Ai CoreBeginnerArtificial IntelligenceMachine Learning

You will learn

  • How to create a custom LLM-as-a-judge evaluation metric (tool-call-accuracy) directly through the AI Core API, including its rating rubric, evaluation steps, and few-shot example.
  • How to configure a prompt-optimization run that uses a custom metric instead of a built-in one.
  • How to normalize a raw BFCL v3 dataset into the optimizer’s “golden record” format and upload it as a dataset artifact.
  • How to register a base prompt, trigger and monitor an optimization execution, and retrieve the optimized prompt.
  • How to compare base vs optimized prompts through live inference, and how the choice of metric (custom vs JSON_Match) shapes the optimizer’s output.
Smita Naik S Smita Naik July 29, 2026
Created by July 29, 2026
Contributors

Prerequisites

Prerequisites

  • You have an SAP AI Core service instance and a service key (with clientid, clientsecret, url, and serviceurls.AI_API_URL).
  • You have the generative-ai-hub-sdk (gen_ai_hub) and ai-api-client-sdk installed, plus requests, python-dotenv, and pydantic.
  • The models gpt-4o:2024-08-06 and gemini-2.5-pro:001 are available in your AI Core tenant (check Generative AI Hub → Models).
  • You have a running orchestration deployment in your tenant (required only for the live comparison in Step 9).
  • You have the BFCL v3 dataset file BFCL_v3_parallel_multiple_10tools.json in your working directory. This is a subset of the Berkeley Function-Calling Leaderboard v3 benchmark.
  • (Optional) You have Bruno installed if you want to follow the REST API option blocks instead of the Python SDK.

Steps

Intro

Large language models are often asked to read a user’s question and decide which tools to call and with what arguments (“tool calling”). A vague system prompt like "You are a helpful assistant." usually makes the model reply in prose instead of structured JSON, which breaks any downstream code expecting machine-readable tool calls.

Prompt Optimization in SAP AI Core automates the trial-and-error of prompt engineering. You give it a starting prompt, a dataset of questions paired with correct tool calls (“golden answers”), and a metric that scores how good a candidate prompt’s output is. The optimizer then iteratively rewrites the prompt, tests it, and keeps the best-scoring version.

This tutorial goes one step further than simply using a pre-existing metric: you will create the custom evaluation metric itself from code, run the full pipeline, and finish by comparing a custom-metric-optimized prompt against a JSON_Match-optimized one to see how the metric choice affects the result.


Step 1 Understand the pipeline (Pre-Read)

Before writing any code, it helps to hold the whole flow in your head. Every step below re-uses a single authenticated client object and builds on variables created earlier, so the notebook cells (and the steps here) are meant to run in order, top to bottom.

The end-to-end pipeline is:

Code
 ┌─────────────────┐     ┌──────────────────┐     ┌────────────────────┐
 │ 1. Connect to    │ --> │ 2. Verify/create  │ --> │ 3. Configure         │
 │    AI Core       │     │  the custom metric │     │  optimization params │
 └─────────────────┘     └──────────────────┘     └────────────────────┘
                                                              │
                                                              v
 ┌─────────────────┐     ┌──────────────────┐     ┌────────────────────┐
 │ 6. Register       │ <-- │ 5. Register base  │ <-- │ 4. Load/normalize +  │
 │  base prompt      │     │  dataset artifact  │     │  upload BFCL data     │
 └─────────────────┘     └──────────────────┘     └────────────────────┘
         │
         v
 ┌─────────────────┐     ┌──────────────────┐     ┌────────────────────┐
 │ 7. Configure +    │ --> │ 8. Retrieve the    │ --> │ 9. Compare base vs   │
 │  run + monitor     │     │  optimized prompt   │     │  optimized (live)     │
 └─────────────────┘     └──────────────────┘     └────────────────────┘

A few SAP AI Core terms used throughout:

TermMeaning
ScenarioA named workflow type registered in AI Core (here genai-optimizations) that groups related artifacts, configurations, and executions.
ArtifactA registered reference to data (here: a folder of dataset files) that executions can read as input.
Prompt RegistryA versioned store for prompt templates, referenced by name + version and updatable by the optimizer.
ConfigurationA saved combination of parameters (metric, models, dataset filenames, prompt reference) describing how an optimization run behaves — but not running it yet.
ExecutionAn actual run of a configuration — the long-running job that performs the optimization.
Golden recordOne row of the evaluation dataset: an input question plus the correct expected output.
Evaluation metricA registered scoring definition — built-in (like JSON_Match) or custom (like tool-call-accuracy) — that the optimizer uses to compare candidate prompts.
LLM-as-a-judgeAn evaluation technique where another LLM reads a candidate output and scores it against a rubric, instead of relying on exact string matching.

All REST calls in the Bruno option blocks assume you have already set the collection variables baseUrl (your AI_API_URL, ending in /v2), token (a valid OAuth bearer token), and resourceGroup (your AI Core resource group). Send these headers on every request unless noted: Authorization: Bearer &#123;&#123;token&#125;&#125;, AI-Resource-Group: &#123;&#123;resourceGroup&#125;&#125;, and Content-Type: application/json.


Step 2 Set up your environment and connect to AI Core
+
Step 3 Verify and create the custom evaluation metric
+
Step 4 Configure the optimization parameters
+
Step 5 Load and normalize the BFCL v3 dataset
+
Step 6 Upload the dataset files and register the artifact
+
Step 7 Register the base prompt template
+
Step 8 Configure, trigger, and monitor the optimization run
+
Step 9 Retrieve the optimized prompt
+
Step 10 Compare base vs optimized prompts via live inference using Python
+
Step 11 Compare the custom metric against `JSON_Match`
+
Step 12 Test yourself
+

Resources

Discussion

Share feedback on this tutorial or join the conversation in SAP Community.

Submit detailed feedback Discuss in Community
Steps
Step 1 of 12
1. Understand the pipeline (Pre-Read) 2. Set up your environment and connect to AI Core 3. Verify and create the custom evaluation metric 4. Configure the optimization parameters 5. Load and normalize the BFCL v3 dataset 6. Upload the dataset files and register the artifact 7. Register the base prompt template 8. Configure, trigger, and monitor the optimization run 9. Retrieve the optimized prompt 10. Compare base vs optimized prompts via live inference using Python 11. Compare the custom metric against `JSON_Match` 12. Test yourself

Learn more →