Skip to main content

Model-agnostic LLM output degeneration detector — 4-signal composite scoring in a single pass

Project description

llm-degen-guard

한국어 문서

Model-agnostic LLM output degeneration detector — 4-signal composite scoring in a single pass.

Install

pip install llm-degen-guard

Quick Start

from degen_guard import DegenGuard

# Batch check
report = DegenGuard.check("Some LLM output text...")
print(report.is_degenerate, report.composite_score)

# Streaming check
guard = DegenGuard()
for chunk in llm_stream:
    is_degen, score = guard.feed(chunk)
    if is_degen:
        print(f"Degeneration detected! score={score:.2f}")
        break

How It Works

Four independent signals are combined into a single composite score (0.0 = normal, 1.0 = degenerate):

Signal Weight What it measures
N-gram diversity 0.35 Distinct 3-gram ratio — low diversity = repetitive
Compression ratio 0.30 zlib compression ratio — highly compressible = repetitive
Substring match 0.20 Exact overlap between first/second half of window
Line diversity 0.15 Unique line ratio — duplicate lines = degenerate

Structural content (code, lists, tables) gets a discount factor to reduce false positives.

Streaming API

guard = DegenGuard(
    window_size=256,       # characters to analyze
    check_interval=64,     # characters between checks
    alert_threshold=3,     # consecutive alerts before flagging
    score_threshold=0.50,  # minimum score to trigger alert
)

for chunk in llm_stream:
    is_degen, score = guard.feed(chunk)
    if is_degen:
        break

guard.reset()  # reuse for next stream

Batch API

report = DegenGuard.check(full_text)

report.is_degenerate      # bool
report.composite_score    # 0.0 ~ 1.0
report.signals            # tuple of SignalDetail
report.structural_penalty # < 1.0 if code/tables detected
report.text_length        # input length

Why not...?

Alternative Limitation llm-degen-guard
Antislop Sampler Needs model weight access (no API support) Works on any output text
SpecRA Paper only, no code available pip install ready
UQLM Requires N generations (cost × N) Single pass, real-time streaming
repetition_penalty Penalizes prompt tokens, causes incoherence Post-generation, no side effects

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm_degen_guard-0.1.1.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_degen_guard-0.1.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file llm_degen_guard-0.1.1.tar.gz.

File metadata

  • Download URL: llm_degen_guard-0.1.1.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for llm_degen_guard-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5ccb655f697baa08f99e560c6b22f328b58a03efceeeeea981d9c934e1b34d91
MD5 af452125f8a6192c1982858703ec3615
BLAKE2b-256 3e8438c2355c3d29f206e40c883012ed10c9f2f7a59c26d4f80657c48461b9d7

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_degen_guard-0.1.1.tar.gz:

Publisher: publish.yml on QuartzUnit/llm-degen-guard

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file llm_degen_guard-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for llm_degen_guard-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 294acf8b13d0e1acef5c68d76cbc4faa5fe665d5861030a1b085255cd099ec49
MD5 44ee87c7144aeb9758fefb65d22a811a
BLAKE2b-256 8886331fbd54567db730f1beeb4bdcbd5e77b61ae7b441390a5f2f563109fcb7

See more details on using hashes here.

Provenance

The following attestation bundles were made for llm_degen_guard-0.1.1-py3-none-any.whl:

Publisher: publish.yml on QuartzUnit/llm-degen-guard

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page