Aerostack
memory

ai-context-window-fit Edge Function — Ai

AI

Trims a conversation message array to fit within a model's context window using configurable strategies, without making any API calls.

navin @navin verified
Updated Mar 12, 2026
GitHub

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.

terminal — aerostack cli
$ npx aerostack add navin/ai-context-window-fit
smart_toy

Use with AI Assistants

MCP

Connect 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.

claude_desktop_config.json
{
  "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
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_exceeded errors.
  • 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-oldest never removes the first system message.
  • summarize-hint inserts a system note informing the model that earlier context was trimmed.
  • maxTokens is capped at the model's absolute context window even if you pass a higher value.

Metadata

upgrade Version 1.0.0
gavel License MIT
language Language typescript
cloud Provider aerostack

Tags

ai
deployed_code

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 →

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.