# Loss function

> Canonical: https://skillalmanac.com/glossary/loss-function | Last verified: 2026-11-24 | Index status: index-eligible

A loss function assigns a numerical penalty to an outcome under a specified objective. Training often minimizes an aggregate of such penalties, possibly with regularization. A small training loss does not establish good performance on new data.

Also called: loss functions, cost function, objective function, loss function machine learning, cost function machine learning.

## In plain language

A model learns by being told how badly it did, and the loss function is the scoring rule that does the telling. Suppose a model predicts house prices. For each house the rule compares the predicted price with the real one and turns the gap into a number. A small total means good predictions and a large total means poor ones. Training then changes the model's internal numbers to push the total down. Optimization aims to reduce the chosen objective, which may differ from the outcome you care about. A rule that barely punishes large mistakes can produce a model that makes them. A low score on training examples also does not prove the model works on new data, so it needs separate testing.

## Technical definition

In supervised learning, a loss can compare predictions with target values; other learning settings define penalties for their own objectives. Per-example losses can be summed or averaged into a training objective, sometimes with regularization. Gradient-based optimization can use derivatives, but not every loss is differentiable and not every optimizer requires gradients.

## How it shows up when you use agents

A loss function appears when you or an agent define an optimization or training task. The model you use was trained against a loss chosen by its builders, which fixed what it was scored on during training. Language models are trained with their own loss (see [Large language model](/glossary/large-language-model) and [RLHF](/glossary/rlhf)).

What this means in practice:

- **A training objective is not a quality guarantee.** A fluent answer can still be wrong; training loss alone does not establish factual accuracy. The safer habit is to give the agent a way to check its work, such as tests, instead of trusting its tone.
- **You still need your own definition of wrong.** Before you change a prompt or a skill, decide what a failure looks like and count it. The evaluation step is yours; the training loss is not.
- **Training loss is not a report card.** A model can score well on its training examples and still do badly on new ones. See [Overfitting](/glossary/overfitting).

If you are tuning an agent setup, the next action is to write down three failure cases and check them after each change.

## Worked example (illustrative)

For two chosen prediction errors, 1 and 3, mean absolute error is (1+3)/2=2, while mean squared error is (1²+3²)/2=5. The sum of squared errors would instead be 10. These calculated penalties use the same errors but different reductions; they are not interchangeable scores or evidence of a useful deployed model.

## Common confusions

- **Loss function versus evaluation metric.** The loss is the number training pushes down. Metrics such as accuracy or a test pass rate are used to judge a model afterwards, and they can disagree with the loss.
- **Loss function versus cost function.** Sources differ on whether the two terms mean the same thing, so check how the one you are reading defines each.
- **Loss versus error in your task.** A model's training loss does not tell you whether it will edit your repository correctly. Tests on your own work do.
- **Loss function versus backpropagation.** The loss measures how wrong a prediction is; backpropagation computes local derivatives that a gradient-based optimizer can use. A finite update need not reduce the loss. See [Backpropagation](/glossary/backpropagation).

## Worked examples in this catalogue

### [Prompt Engineering for Agents vs Chat Models](https://skillalmanac.com/learn/prompt-engineering-guide-for-agents)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: The guide compares prompting an agent with prompting a chat model, and cites Anthropic's advice to have success criteria and a way to test them before improving a prompt.

Why it exercises the concept: Defining what counts as wrong before you optimise is the idea behind a loss function. Here a person sets the test, where in training the formula is built into the training code.

### [llm-eval-test-sets](https://skillalmanac.com/skills/skill-almanac/llm-eval-test-sets)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: Builds and validates an eval dataset for any LLM feature, with a checker for duplicates, missing graders, imbalance and leakage.

Why it exercises the concept: An eval set scores a finished feature on cases you chose. It is a separate measurement from the training loss, and keeping the two apart is the point of the contrast.

### [llm-json-output-validator](https://skillalmanac.com/skills/skill-almanac/llm-json-output-validator)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: Validates LLM replies against a JSON Schema subset and tallies failures by class and field.

Why it exercises the concept: It turns 'wrong' into a count you can track, a task-level counterpart to a loss: a defined rule for what an error is.

## Related terms

- [Machine learning (ML)](https://skillalmanac.com/glossary/machine-learning.md)
- [Neural network](https://skillalmanac.com/glossary/neural-network.md)
- [Backpropagation](https://skillalmanac.com/glossary/backpropagation.md)
- [Gradient descent](https://skillalmanac.com/glossary/gradient-descent.md)
- [Supervised learning](https://skillalmanac.com/glossary/supervised-learning.md)
- [Overfitting](https://skillalmanac.com/glossary/overfitting.md)
- [Cross-entropy loss](https://skillalmanac.com/glossary/cross-entropy-loss.md)

## Sources

- [What is a loss function?](https://www.ibm.com/think/topics/loss-function), IBM (accessed 2026-11-24)
- [Linear regression: Loss](https://developers.google.com/machine-learning/crash-course/linear-regression/loss), Google for Developers (accessed 2026-11-24)
- [What is backpropagation?](https://www.ibm.com/think/topics/backpropagation), IBM (accessed 2026-11-24)

Definitions are written by Skill Almanac from the sources above and checked against them on 2026-11-24. [Glossary index](https://skillalmanac.com/glossary.md).
