truncate

package
v1.6.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 12, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package truncate provides text truncation utilities for managing LLM context.

When building prompts for language models, text often needs to be truncated to fit within token limits. This package provides flexible truncation with multiple strategies.

Strategies

Three truncation strategies are available:

  • FromEnd: Remove content from the end (default)
  • FromMiddle: Remove content from the middle, keeping start and end
  • FromStart: Remove content from the start

Basic Usage

Create a truncator and truncate text:

tr := truncate.NewFromEnd()
result, truncated := tr.Truncate("very long text...", 100)

Or use a specific strategy:

tr := truncate.New(truncate.FromMiddle)
result, truncated := tr.Truncate(text, maxTokens)

Custom Token Counter

By default, truncation uses an estimating counter (~4 chars/token). For more accurate results, provide a custom counter:

tr := truncate.NewFromEnd().WithCounter(myCounter)

Convenience Functions

For simple one-off truncation:

result := truncate.ToTokens(text, 100)   // Truncate to 100 tokens
result := truncate.ToLines(text, 50)     // Truncate to 50 lines
result := truncate.ToLength(text, 500)   // Truncate to 500 characters
result := truncate.Smart(text, 500)      // Truncate at word boundaries

UTF-8 Support

All truncation properly handles UTF-8 text, counting runes rather than bytes to ensure multi-byte characters are not split.

Index

Constants

View Source
const DefaultEndSuffix = "..."

DefaultEndSuffix is the default suffix for end truncation.

View Source
const DefaultMiddleSuffix = "\n...[content truncated]...\n"

DefaultMiddleSuffix is the default suffix for middle truncation.

View Source
const DefaultStartSuffix = "..."

DefaultStartSuffix is the default suffix for start truncation.

Variables

This section is empty.

Functions

func Smart

func Smart(text string, maxLen int) string

Smart attempts to truncate at word or sentence boundaries. Falls back to hard truncation if no good break point is found.

func ToLength

func ToLength(text string, maxLen int) string

ToLength truncates text to a maximum character length. Properly handles UTF-8 by counting runes, not bytes.

func ToLines

func ToLines(text string, maxLines int) string

ToLines truncates text to a maximum number of lines.

func ToTokens

func ToTokens(text string, maxTokens int) string

ToTokens truncates text to fit within the specified token limit. Uses end truncation with the default estimating counter.

Types

type Strategy

type Strategy int

Strategy defines how text is truncated.

const (
	// FromEnd removes content from the end (default).
	FromEnd Strategy = iota

	// FromMiddle removes content from the middle, keeping start and end.
	FromMiddle

	// FromStart removes content from the start.
	FromStart
)

type Truncator

type Truncator struct {
	// contains filtered or unexported fields
}

Truncator truncates text to fit within token limits.

func New

func New(strategy Strategy) *Truncator

New creates a truncator with the given strategy.

func NewFromEnd

func NewFromEnd() *Truncator

NewFromEnd creates a truncator that removes content from the end.

func NewFromMiddle

func NewFromMiddle() *Truncator

NewFromMiddle creates a truncator that removes content from the middle.

func NewFromStart

func NewFromStart() *Truncator

NewFromStart creates a truncator that removes content from the start.

func (*Truncator) Strategy

func (t *Truncator) Strategy() Strategy

Strategy returns the truncator's strategy.

func (*Truncator) Suffix

func (t *Truncator) Suffix() string

Suffix returns the truncator's suffix.

func (*Truncator) Truncate

func (t *Truncator) Truncate(text string, maxTokens int) (string, bool)

Truncate reduces the text to fit within the token limit. Returns the truncated text and whether truncation occurred.

func (*Truncator) WithCounter

func (t *Truncator) WithCounter(counter tokens.Counter) *Truncator

WithCounter sets a custom token counter.

func (*Truncator) WithSuffix

func (t *Truncator) WithSuffix(suffix string) *Truncator

WithSuffix sets a custom suffix for truncation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL