Skip to main content
The rewards module provides customizable reward functions for various task domains including math, code, and search.

RewardFunction

Protocol defining the interface for reward functions.
dict
Task dictionary containing question, answer, and other metadata.
str | Action
Agent’s response/solution. Can be a string or Action object.
RewardOutput
Reward output containing reward value and metadata.

RewardOutput

Dataclass for reward function results.
float
The computed reward value (typically 0.0 or 1.0).
dict
Additional information about the evaluation.

Built-in Reward Functions

math_reward_fn

Reward function for mathematical reasoning tasks.
Supported task fields:
  • answer or ground_truth: Expected answer
  • data_source: Dataset identifier (optional)

code_reward_fn

Reward function for code generation tasks with execution-based evaluation.
Supported task fields:
  • test_cases: List of input/output test pairs
  • entry_point: Function name to test
  • timeout: Execution timeout in seconds (optional)

search_reward_fn

Reward function for information retrieval and question answering tasks.
Supported task fields:
  • ground_truth or answer: Expected answer
  • supporting_facts: Supporting evidence (optional)
  • data_source: Dataset identifier (optional)

f1_reward_fn

Generic F1 score-based reward function using token overlap.
Features:
  • Normalizes text (lowercase, remove punctuation/articles)
  • Computes token-level precision and recall
  • Returns F1 score as reward

zero_reward

Placeholder reward function that always returns 0.

Custom Reward Functions

Example: Custom Keyword Reward


Example: Custom Length Penalty


Example: Multi-Criteria Reward


Using Rewards in an AgentFlow

Modern agents use the Evaluator protocol — a separate function that reads the Episode produced by an AgentFlow and returns an EvalOutput. The reward functions in rllm.rewards are wrapped inside the evaluator:
See cookbooks/math/math_eval.py for the full version that this snippet is adapted from.

Using Rewards with Workflows

For the legacy Workflow path, the reward function is passed in directly: