An educational local-first C# project showing how deterministic context budgeting keeps prompts within a fixed token budget while preserving required business context.
This version uses an e-commerce support scenario (late + damaged delivery refund resolution).
This repository demonstrates a practical deterministic prompt-packing flow:
- Reserve output tokens.
- Reserve fixed prompt overhead.
- Compute remaining context token budget.
- Sort candidate context blocks deterministically:
- required blocks first
- higher priority first
- newer observation timestamp first
- block ID as final tie-break
- Include blocks while budget remains.
- Exclude overflow blocks with explicit reasons.
- Compose the final prompt and optionally invoke a local OpenAI-compatible model.
If any required block cannot fit, the app marks the result as CanProceed = false and exits before model invocation.
- Deterministic context selection independent of input order
- Explicit budget accounting (
context window - reserved output - fixed prompt) - Required vs optional block handling with typed exclusion reasons
- Heuristic token estimation for practical local budgeting
- Prompt composition that preserves packed block order
- Optional local model invocation via Semantic Kernel + OpenAI-compatible endpoint
- Unit tests for packing stability, budget behavior, and prompt ordering
- .NET 10 SDK or later
- Optional for model calls:
- LM Studio OpenAI-compatible server (
http://localhost:1234/v1) - or Ollama OpenAI-compatible endpoint (
http://localhost:11434/v1)
- LM Studio OpenAI-compatible server (
From the project root:
dotnet test DeterministicContextBudgeting.slnxRun budgeting + prompt preview only (no model call):
set DCB_App__EnableModelCall=false
dotnet run --project DeterministicContextBudgetingRun with model invocation enabled:
set DCB_App__EnableModelCall=true
dotnet run --project DeterministicContextBudgetingDefault settings are in DeterministicContextBudgeting/appsettings.json under App.
Example:
{
"App": {
"ModelContextWindowTokens": 850,
"ReservedOutputTokens": 300,
"FixedPromptTokens": 220,
"Provider": "lmstudio",
"BaseUrl": "http://localhost:1234/v1",
"ApiKey": "not-needed",
"ModelId": "deepseek/deepseek-r1-0528-qwen3-8b",
"Temperature": 0.0,
"EnableModelCall": true
}
}Environment variable overrides use prefix DCB_:
DCB_App__ModelContextWindowTokensDCB_App__ReservedOutputTokensDCB_App__FixedPromptTokensDCB_App__ProviderDCB_App__BaseUrlDCB_App__ApiKeyDCB_App__ModelIdDCB_App__TemperatureDCB_App__EnableModelCall
Ollama example:
set DCB_App__Provider=ollama
set DCB_App__BaseUrl=http://localhost:11434/v1
set DCB_App__ApiKey=ollama
set DCB_App__ModelId=mistral:7b
set DCB_App__EnableModelCall=true
dotnet run --project DeterministicContextBudgetingEcommerceSupportScenariocreates realistic support context blocks.ContextBudgeterestimates token cost and packs deterministically.ContextBudgetingResultrecords included/excluded blocks and proceed gating.PromptComposerbuilds the final prompt from included blocks.Program.csprints budget summary and optionally calls the configured local model.
.
+-- DeterministicContextBudgeting.slnx
+-- DeterministicContextBudgeting/
| +-- DeterministicContextBudgeting.csproj
| +-- Program.cs
| +-- appsettings.json
| +-- App/
| | +-- AppConfig.cs
| | +-- EcommerceSupportScenario.cs
| +-- Budgeting/
| | +-- ContextBudgeter.cs
| | +-- HeuristicTokenEstimator.cs
| | +-- ITokenEstimator.cs
| +-- Domain/
| | +-- ContextBlock.cs
| | +-- PromptBudgetRequest.cs
| | +-- ContextBudgetingResult.cs
| | +-- PackedContextBlock.cs
| | +-- ExcludedContextBlock.cs
| +-- Prompting/
| +-- PromptComposer.cs
+-- DeterministicContextBudgeting.Tests/
| +-- DeterministicContextBudgeting.Tests.csproj
| +-- ContextBudgeterTests.cs
| +-- PromptComposerTests.cs
+-- LICENSE
+-- README.md
- Stable inclusion order for equivalent candidates
- Required overflow is explicit and blocks proceed (
CanProceed = false) - Budget invariants are validated (
used + remaining = available) - Prompt block order exactly follows packed block order
See the LICENSE file for details.
Contributions are welcome for improvements within current project scope.
Suggested areas:
- Better token estimation strategies with model-specific calibration
- Additional business scenarios beyond e-commerce support
- More deterministic tie-break and overflow policy variants
- Expanded tests for edge cases and larger context sets