Course contentsShow
← AI Engineering
Lesson 1487 of 1,88636. Security in AI SystemsFree lesson

Input Length and Token Limits

Enforce maximum input lengths and token budgets to prevent resource exhaustion attacks.

Input Length and Token Limits

What you'll learn: How enforcing strict input length and token budgets prevents attackers from exhausting system resources and degrading service availability.

Why Input Limits Matter for Security

Attackers can deliberately send extremely long inputs to AI systems—massive prompts, repeated text, or nested instructions—to consume compute resources, blow up processing costs, and potentially crash your service. This is a resource exhaustion attack, similar to how traditional web services face denial-of-service threats.

LLM inference costs scale with token count. An attacker discovering your endpoint could send 100,000-token inputs repeatedly, draining your API budget in minutes or causing latency spikes that hurt legitimate users.

Core Protection Strategies

Set hard token limits before processing begins. Use your provider's tokenizer (like OpenAI's tiktoken) to count tokens before sending to the model:

import tiktoken

MAX_INPUT_TOKENS = 4000

def validate_input_length(user_input: str) -> bool:
    encoder = tiktoken.encoding_for_model("gpt-4")
    token_count = len(encoder.encode(user_input))
    return token_count <= MAX_INPUT_TOKENS

Character-level checks provide a fast first line of defense before tokenization. Reject inputs exceeding reasonable character counts (e.g., 10,000 characters) immediately.

Budget per request and per user: Track cumulative token usage across API calls. Implement rate limits that consider both request count and total tokens consumed per time window.

Implementation Layers

Validate at multiple points: API gateway (character count), application layer (token count), and set max_tokens parameters in your LLM API calls to cap both input and output.

Key Takeaway: Enforcing token and length limits transforms resource exhaustion from an easy attack vector into a controlled, predictable constraint—protecting both your infrastructure and budget from malicious or accidental abuse.