Skip to content
DenseChip
Learn
PracticeCode labInterview

DenseChip

Navigate

Menu

LearnPracticeCode labInterview

Tools

All toolsSV editorCode repoResume
DenseChip

Practice, courses, and chip-design tools in one focused workspace.

LearnPracticeCode labInterviewToolsLearning paths

© 2026 DenseChip

AboutContactPrivacyTerms
Skip to lesson

Course contents

  • Getting Started with AI for VLSI Engineers5
  • Just Enough AI: Models and Context15
    • What a Language Model Does
    • Tokens and Tokenization
    • Context Window and Attention for Users
      • What Counts Against the Window
      • When the Window Fills
      • Attention in One Picture
      • Long Inputs and the Lost Middle
      • A Context-Budget Planner
  • Prompting13
  • Trust and Verification14
  • Using AI for Engineering Work17
  • Engineering Walkthroughs7
  • Practical Foundations for AI Workflows4
  • Engineering Data5
  • Retrieval-Augmented Generation6
  • Agents and Tool Use18
  • Agent Design8
  • MCP and Skills6
  • Multi-Agent Design6
  • Practical Capstone6
  • Tokens, Cost and Latency12
  • Agentic Workflows for Chip Teams18
  • Interview and Judgment10
  • AI Skills: Full Course Index
  • Working with AI Glossary
  • Working with AI Cheatsheet
Loading lesson

‹Context Window and Attention for Users
  1. Topics
  2. AI Skills for VLSI Engineers
  3. Just Enough AI: Models and Context
  4. Context Window and Attention for Users
  5. A Context-Budget Planner

Just Enough AI · Beginner to Intermediate

A Context-Budget Planner

A small deterministic planner that checks whether a context budget fits.

On this page

    The planner idea

    A planner lists every budget item with an EXAMPLE chars-per-token rate and adds them up. It compares the total with the budget and proposes a trim when the plan is over.

    The rates below are EXAMPLE parameters, not facts about any real tokenizer. Shuffling the sections never changes the total, which the program checks.

    The program

    The program below is runnable and uses only the standard library. It estimates four sections, checks the arithmetic against hand-computed constants, and prints one PASS line when every check holds.

    example.py
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    import random
    import sys
    
    # EXAMPLE estimator parameters, not facts about any real tokenizer.
    CHARS_PER_TOKEN_PROSE = 4
    CHARS_PER_TOKEN_CODE = 3
    BUDGET_TOKENS = 2000
    
    rng = random.Random(11)
    
    SECTIONS = [
        ("system-prompt", 400, CHARS_PER_TOKEN_PROSE),
        ("recent-chat", 3000, CHARS_PER_TOKEN_PROSE),
        ("spec-extract", 2400, CHARS_PER_TOKEN_CODE),
        ("answer-reserve", 2000, CHARS_PER_TOKEN_PROSE),
    ]
    
    
    def estimate_tokens(n_chars, chars_per_token):
        return (n_chars + chars_per_token - 1) // chars_per_token
    
    
    def plan_total(sections):
        return sum(estimate_tokens(n, c) for _, n, c in sections)
    
    
    bad = []
    
    estimates = [estimate_tokens(n, c) for _, n, c in SECTIONS]
    if estimates != [100, 750, 800, 500]:
        bad.append("section-estimates")
    
    total = plan_total(SECTIONS)
    if total != 2150:
        bad.append("total")
    
    remaining = BUDGET_TOKENS - total
    if remaining != -150:
        bad.append("remaining")
    if (remaining >= 0) != (total <= BUDGET_TOKENS):
        bad.append("fits-flag")
    
    shuffled = list(SECTIONS)
    rng.shuffle(shuffled)
    if plan_total(shuffled) != 2150:
        bad.append("order-check")
    
    trimmed = [s for s in SECTIONS if s[0] != "recent-chat"]
    trimmed_total = plan_total(trimmed)
    trimmed_remaining = BUDGET_TOKENS - trimmed_total
    if trimmed_total != 1400:
        bad.append("trimmed-total")
    if trimmed_remaining != 600:
        bad.append("trimmed-remaining")
    
    if bad:
        print("FAIL: " + str(len(bad)) + " budget checks wrong: " + ",".join(bad))
        sys.exit(1)
    print("PASS: 4 sections need 2150 tokens of a 2000-token budget; trimmed plan needs 1400")

    Reading its answer

    The full plan needs 2150 tokens against a 2000-token budget, so it is 150 over. Dropping the recent chat leaves 1400 tokens with 600 to spare.

    The order check proves the total is order-independent. Budgeting is commutative even though attention quality is not.

    Common mistakes

    Common pitfall

    Trusting the EXAMPLE rates as real tokenizer facts breaks every plan. The rates are clearly labelled parameters; confirm real counts with the tool before sending.

    Interview

    Likely probes: what the planner proves and what it assumes.

    • It proves the EXAMPLE arithmetic is internally consistent, nothing about a real model.

    • Totals do not depend on section order; fit depends on the budget chosen.

    • A negative remainder means trim or summarize, never send and hope.

    Check yourself The planner reports a remainder of -150. What does the negative sign mean, and what fixes it?

    The plan exceeds the budget by 150 tokens. Trim a section or summarize history until the remainder is not negative.

    Check yourself Why does the program shuffle the sections and re-add them?

    To prove the total does not depend on order. The shuffled total must still equal 2150.

    Related topics

    • Long inputs and the lost middle

    • Cost arithmetic

    • Prompt debugging

    Previous

    20 / 173

    Next: Prompting

    Up next

    Up nextPrompting

    Sign in to save progress

    Practice this lessonReport an error

    See also

    • What Counts Against the Window
    • When the Window Fills
    • Attention in One Picture