ai-context-window-fit Edge Function — Ai
AITrims a conversation message array to fit within a model's context window using configurable strategies, without making any API calls.
Edge function ai-context-window-fit Trims a conversation message array to fit within a model's context window using configurable strategies, without making any API calls.. Deployed on Cloudflare Workers — zero cold starts, globally distributed. Mount it via your Aerostack workspace to call it from any AI agent.
npx aerostack add navin/ai-context-window-fit Use with AI Assistants
MCPConnect Claude, Cursor, or any MCP-compatible client — then call this function by slug
① Add MCP Server
Add this once — access all Aerostack functions from your AI tool.
{
"mcpServers": {
"aerostack": {
"url": "https://mcp.aerostack.dev",
"type": "http"
}
}
} ② Call this function
Ask your AI to use the call_function tool with this slug:
call_function({
slug: "ai-context-window-fit",
args: {
"messages": null,
"maxTokens": 42,
"model": "gpt-4",
"strategy": "trim-oldest",
"reserveTokens": 0
}
}) ai-context-window-fit — Trim conversation history to fit a model's context window
Removes messages from a conversation array to fit within a token budget, using configurable strategies to decide which messages to drop — no LLM API calls required.
API
POST /api/ai-context-window-fit
Request body
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
messages |
{role: string, content: string}[] |
✅ | — | Conversation messages |
maxTokens |
number | ✅ | — | Maximum token budget |
model |
"gpt-4" | "gpt-3.5-turbo" | "claude-3" | "claude-2" | "gemini-pro" |
❌ | "gpt-4" |
Model for window ceiling |
strategy |
"trim-oldest" | "trim-middle" | "summarize-hint" |
❌ | "trim-oldest" |
Trimming strategy |
reserveTokens |
number | ❌ | 0 |
Tokens reserved for completion |
Success response (200)
{
"success": true,
"data": {
"messages": [{ "role": "system", "content": "..." }, { "role": "user", "content": "..." }],
"estimatedTokens": 1240,
"removedCount": 5,
"fitsInWindow": true
}
}
Error responses
| Code | HTTP | When |
|---|---|---|
INVALID_INPUT |
400 | Missing required field or wrong type |
INTERNAL_ERROR |
500 | reserveTokens leaves no room |
Usage
cURL
curl -X POST "$FUNCTION_URL" \
-H "Content-Type: application/json" \
-d '{
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Tell me about AI."},
{"role": "assistant", "content": "AI is..."}
],
"maxTokens": 4000,
"strategy": "trim-oldest",
"reserveTokens": 500
}'
TypeScript / JavaScript (HTTP)
const response = await fetch(FUNCTION_URL, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ messages, maxTokens: 8000, strategy: 'trim-middle' }),
});
const { data } = await response.json();
const safeMessages = data.messages;
Direct import (Node / Bun / Deno)
import { aiContextWindowFit } from '@aerostack/functions/ai-context-window-fit';
const { messages, estimatedTokens } = aiContextWindowFit({
messages: conversationHistory,
maxTokens: 16000,
reserveTokens: 2000,
});
Use Cases
- Trimming a growing chat history before each LLM API call to avoid
context_length_exceedederrors. - Building multi-turn agents that need to maintain recent context while staying within token limits.
- Estimating token usage of a conversation array without making any API calls.
Notes
- Token estimation uses ~4 chars/token + 4 tokens per message for role/formatting overhead.
trim-oldestnever removes the firstsystemmessage.summarize-hintinserts a system note informing the model that earlier context was trimmed.maxTokensis capped at the model's absolute context window even if you pass a higher value.
Metadata
Tags
Publisher
@navin verified
Build and publish your own functions
Write a TypeScript function, deploy it to the edge, and share it with thousands of developers — in minutes.
More AI Functions
Browse AI Functions →ai-chunk-overlap
by @navin
Splits text into overlapping chunks with configurable size, overlap, and boundary snapping (char, word, sentence) for RAG and embedding pipelines.
ai-cost-estimate
by @navin
Calculates the API cost for an LLM request given a model name, prompt token count, and completion token count, supporting multiple currencies.
ai-extract-keywords
by @navin
Extracts the top N keywords from text using TF-IDF inspired scoring with built-in English stopword filtering, no external API calls required.
ai-guardrail-injection-detect
by @navin
Scores text for common prompt injection attack patterns including role overrides, instruction leaking, and jailbreak attempts.
ai-language-detect
by @navin
Detects the natural language of a text string using character trigram frequency analysis, supporting 13 languages with no external API calls.
ai-messages-to-prompt
by @navin
Serialises a structured message array into a formatted prompt string for open-source LLMs, supporting ChatML, Llama 2, Alpaca, and plain text formats.
Frequently asked questions
What does the ai-context-window-fit function do? +
ai-context-window-fit is a serverless edge function for ai automation written in aerostack. Deploy it to Cloudflare Workers via your Aerostack workspace.
How do I deploy the ai-context-window-fit function? +
Install the Aerostack CLI and run: ```bash aerostack deploy function @navin/ai-context-window-fit ``` It will be live on Cloudflare Workers in seconds.
What runtime does ai-context-window-fit use? +
ai-context-window-fit runs on aerostack on the Cloudflare Workers edge runtime — zero cold starts, globally distributed.
Can I customise the ai-context-window-fit function? +
Yes. Fork the function from your Aerostack dashboard, modify the source, and redeploy. All changes are version-controlled.