From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=systemreboot.net; s=default; t=1794718077; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=FlWCzERjLUtvcOz7j9DeEI+1VDIvtZUtmuAfctCgIM0=; b=GfuuLRoLYu0ckf1YWoqKJW6ZHeajyLMQW9kCk6hN+WiG5PAaaVlMyCjqQ44VmG35TT9Sqa 35F6w7gi3l4fgbHulM/Akv2Z6NbXDwQgqWlPlVOERHcDVz7r7adnZa9VmXVxeWmE68rFHo PNR6y4XGjpxoI+gLC7bavjIH112Upp9FsOT8XKh1bruQzkH4D153EdEYstEmFRP43+i8H9 kcTj727yJAx7by1xZJ+B8nYymTnmAXQXiIWRguSnHGs2xGgcxlvBiiArXfijtbKnA2kayB LPApoZQQLVKftZj2EcU1vRAFpQgcORVjALSglPbPDbOu65DJHeBN97mj4/Cyvw== Received: from steel ( [192.168.1.1]) by hrrol.systemreboot.net (OpenSMTPD) with ESMTPSA id 987ea509 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Sun, 15 Nov 2026 04:47:57 +0000 (UTC) From: Arun Isaac To: Arun Isaac , Ricardo Wurmus Cc: kaagum@systemreboot.net Subject: [PATCH v2 5/5] Allow user-defined context compacters. Date: Sun, 15 Nov 2026 04:47:50 +0000 Message-ID: <20261115044752.5595-6-arunisaac@systemreboot.net> X-Mailer: git-send-email 2.54.0 In-Reply-To: <87ik20nycd.fsf@systemreboot.net> References: <87ik20nycd.fsf@systemreboot.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: --- README.md | 15 +++++++++++++++ bin/kaagum | 12 +++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 210089a..19a6d39 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,21 @@ noop-compacter ``` The noop compacter is an identity compacter. It doesn't compact at all; use this compacter to disable context compaction. To use this compacter, pass `--compacter-eval=noop-compacter` to kaagum. +## User-defined compacters + +kaagum also supports custom user-defined compacters. Similar to the `--more-tools` parameter, you define your custom compacter in a scheme file and pass it to kaagum using the `--compacter-file` parameter. + +A compacter is a function that accepts a list of `messages` and returns a compacted list of messages. `messages` is a list of ``, `` or `` records. See `kaagum/message.scm` for definitions of these record types and utilities to operate on them. + +In addition, compacters are also provided the following keyword arguments: +- **tokens:** number of tokens that correspond to all `messages` put together +- **context-length:** context length of the model in use +- **llm-base-uri:** base URI of the LLM provider +- **llm-api-key:** API key to authenticate with the LLM provider +- **model:** name of the model in use + +See definitions of built-in compacters in `kaagum/compacters.scm` for examples. + # Contributing Please report issues or send patches to the public inbox at [kaagum@systemreboot.net](https://lists.systemreboot.net/kaagum/). diff --git a/bin/kaagum b/bin/kaagum index 50fbb76..ac5ab4d 100755 --- a/bin/kaagum +++ b/bin/kaagum @@ -57,6 +57,9 @@ exec guile --no-auto-compile -e main -s "$0" "$@" (option (list #\c "compacter-eval") #t #f (lambda (opt name arg result) (acons 'compacter-eval arg result))) + (option (list "compacter-file") #t #f + (lambda (opt name arg result) + (acons 'compacter-file arg result))) (option (list "kagi-api-key-command") #t #f (lambda (opt name arg result) (acons 'kagi-api-key-command arg result))) @@ -84,6 +87,7 @@ Run kaagum AI agent. (default: \"anthropic/claude-opus-4.8\") -t, --more-tools=FILE scheme file providing more tools to the agent -c, --compacter-eval=EXPR expression returning compacter (default: noop) + --compacter-file=FILE scheme file providing compacter --kagi-api-key-command=COMMAND command to run to get Kagi API key @@ -142,9 +146,11 @@ Run kaagum AI agent. (if (assq-ref args 'more-tools) (load (assq-ref args 'more-tools)) '())) - (eval (call-with-input-string (assq-ref args 'compacter-eval) - read) - (interaction-environment))))) + (if (assq-ref args 'compacter-file) + (load (assq-ref args 'compacter-file)) + (eval (call-with-input-string (assq-ref args 'compacter-eval) + read) + (interaction-environment)))))) (parameterize ((%kagi-api-key (and kagi-api-key-command (get-api-key kagi-api-key-command)))) ;; (trace 'kagi "Key ~a" (%kagi-api-key)) -- 2.54.0