Skip to main content

Goals

  • Learn why we often need language model graders
  • Understand how to integrate them into an environment

Prerequisites

  • An OpenReward account
  • An OpenReward API key
  • An API key and SDK for your model provider of choice (e.g. OpenAI, Anthropic, Google, OpenRouter)

Introduction

Many tasks can be easily verified with rules-based parsers. For example, in a task where the answer is an integer (e.g. 4) and there is a submit_answer tool, we can simply compare the model answer to the ground truth, e.g. model_answer == ground_truth_answer and assign a reward accordingly. But other tasks are harder to grade with simple rules. Consider a medical question where the model answer is acetaminophen but the ground truth answer is paracetamol. If we did string matching, we wouldn’t find a match - but it turns out these are two different names for the same drug. For this reason, we often use LLM graders to assign reward. We pass the question, the model answer and the ground truth - along with any other instructions - into a language model and ask it to identify whether the answer is correct. This is more expensive than a rules-based parser, but it is more general in that it uses the knowledge of a language model to help assign a reward. Graders can give a binary response, such as yes or no, but can also give partial credit. Often partial credit is helpful in reinforcement learning as it helps the model improve without getting the full solution.

Example

In this tutorial we’ll see how to set up a binary grader with the drug name example mentioned in the introduction. First, make sure you have the openreward library installed:
To begin we’ll initialise our project with a basic template:
We’ll replace the train_tasks and test_tasks with the following:
Now we’ll need to define an LLM grader. For this tutorial we’ll use an OpenAI grader, but you can choose any grader of your choice. We’ll import the AsyncOpenAI client and rewrite our __init__.py:
Now let’s make a function outside our environment client:
Here we pass in a model_response and a solution, ask a model to grade it, and then return a JSON response with a score key. Next we can rewrite the answer tool:
And now we can test. Our full server.py is:
To sample, run the following code:
1

Set your API keys

Make sure you have API keys for OpenReward and OpenAI, and set these as environment variables:
2

Create your code

Save this as quickstart.py:
3

Run your code

Example output:
You should note that because the grader requires an OpenAI API key, we pass in the key as follows:
If you are using a grader from another provider, your code will follow the same pattern. You will need to capture this secret server-side as well so the key from the client can be passed into the grader on the server backend:

Advanced Graders

In the previous example, we asked the grader to output a score alone. In some cases we may want the agent to do some limited thinking before outputting this score, or at least offer transparent reasoning that leads to its score. To see how we can do this, let’s first rewrite the grader:
And let us also rewrite the answer tool:
Now rerun quickstart.py:
As we can see we have the same result but now the agent is incentivised to spend tokens doing some reasoning before answering. In general, you should test different graders and observe their outputs and decide which one better matches human judgement. The direct grader is cheaper in that it spends fewer tokens, but if you need higher grader accuracy you may want to opt for graders like this. A more expensive grader entirely involves using a reasoning model to judge the output. This is essentially a more extreme version of the grader we have just made, where a model will spend many more tokens thinking before giving a reward. These types of graders are also called “generative verifiers”. We can use one of these graders by changing our original grader to the following:
Most model providers hide the reasoning, so it won’t be visible in this case. Note that a full reasoning model is not needed for verification given the simplicity of this task, but there may be harder verification problems where they may be useful. The Rubrics tutorial gives some examples of these type of problems.

Next Steps

Using Rubrics

Learn how to use rubrics for harder-to-verify domains and partial credit scoring