Skip to content
Back to skills

Head Tail Truncation

ASecurity

Preserves both the start and end of long text sequences when truncating for transformer input limits.

  • 61 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 12, 2026
developmentpythongo

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill head-tail-truncation --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Head Tail Truncation?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Head Tail Truncation
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-head-tail-truncation/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-head-tail-truncation)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: nlp-head-tail-truncation
description: >
  Preserves both the start and end of long text sequences when truncating for transformer input limits.
---
# Head-Tail Truncation

## Overview

Standard BERT truncation drops tokens from the end, losing potentially important closing content. Head-tail truncation keeps the first N and last M tokens, discarding the middle. This preserves both the introduction and conclusion of long documents, which often carry the most signal.

## Quick Start

```python
def head_tail_truncate(token_ids, max_length, head_ratio=0.5):
    """Keep head and tail tokens, drop the middle.

    Args:
        token_ids: list of token IDs (without special tokens)
        max_length: maximum allowed tokens (excluding [CLS]/[SEP])
        head_ratio: fraction of budget allocated to head

    Returns:
        truncated token ID list
    """
    if len(token_ids) <= max_length:
        return token_ids

    head_len = int(max_length * head_ratio)
    tail_len = max_length - head_len
    return token_ids[:head_len] + token_ids[-tail_len:]
```

## Workflow

1. Tokenize input text into subword tokens
2. If length exceeds budget, split budget between head and tail
3. Concatenate head tokens + tail tokens
4. Add special tokens ([CLS], [SEP]) around the result
5. For multi-segment inputs, apply per-segment or with shared budget

## Key Decisions

- **Head ratio**: 0.5 is default; bias toward head for Q&A, toward tail for conclusions
- **Per-segment budgets**: For question+answer pairs, allocate separate budgets per segment
- **vs sliding window**: Head-tail is simpler and often sufficient; sliding window adds complexity
- **Middle content**: If middle matters (e.g., evidence paragraphs), use hierarchical encoding instead

## References

- Google QUEST Q&A Labeling competition, 1st place solution (Kaggle)
- Source: [1st-place-solution](https://www.kaggle.com/code/ddanevskyi/1st-place-solution)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.