﻿# Context-aware Prompt Tuning: refining the embeddings of few-shot context

Understand CPT context-token masks, constrained embedding changes, recency weighting and the distinction between optimizing context and changing the base model.

Author: Retrivium editorial team
Updated: 2026-11-03
Canonical URL: https://retrivium.ai/journal/guide-cpt-context-embedding-tuning/
Language: en

Context-aware Prompt Tuning, abbreviated CPT here, refines the numerical context built from a small collection of demonstrations. Selected context embeddings are optimized while the base model stays frozen. The method combines the example-based setup of in-context learning with a bounded parameter-learning step.

## Identify which context positions can change

A demonstration contains input text, labels and formatting boundaries with different roles. PEFT exposes token and type masks for selecting context behavior. Inspect these masks against the rendered token sequence before training. A shifted boundary can modify a label or formatting position different from the one the experiment intended, even when all tensors have valid dimensions.

## Understand the constrained optimization

The original method uses projected optimization to keep adjusted embeddings near their starting values. Its adversarial-method inspiration concerns how numerical input changes are controlled; the task objective is minimized. The perturbation allowance is therefore part of the model choice. Compare it deliberately rather than treating a large allowed change as automatically equivalent to a better learned prompt.

## Account for demonstration order

Examples near the end of context can have different influence from earlier ones. The implementation offers loss weighting intended to address that recency issue. Record the demonstration order and weighting policy together. For an illustrative few-shot classifier, compare reasonable orderings on independent inputs rather than judging the method from one favorable arrangement of examples.

## Keep the evaluation boundary clear

Optimization of context is still adaptation, even with frozen base weights. Do not use final held-out examples to select perturbation limits, demonstration order or a checkpoint and then describe their score as untouched evaluation. Preserve a separate selection stage. Report the task labels and decoding rule, since changing label spelling or parsing can affect a classification result independently of the learned embeddings.

## Preserve the full context artifact

Save the initial token identities, masks, learned context state, projection settings, tokenizer and base revision. Reload through the intended generation path and compare fixed inputs. The maintained PEFT example supplies a concrete training workflow, not proof that a plain text-message API accepts the same numerical context. CPT is useful when its small-data adaptation and the resulting held-out behavior can be reproduced with an explicit context contract.

## Primary references

[PEFT: Context-aware Prompt Tuning](https://huggingface.co/docs/peft/package_reference/cpt).

[CPT original research](https://arxiv.org/abs/2410.17222).

[PEFT CPT maintained example](https://github.com/huggingface/peft/blob/main/examples/cpt_finetuning/README.md).

Related explanations: [embedding and hidden-state distinctions](/journal/static-token-embedding-contextual-state/); [independent holdout design](/journal/document-level-data-split-leakage/).
