Thegan › Documentation

Documentation

Thegan is a single binary with three modes. Everything is configured through environment variables; the full reference lives in the README.

Usage

thegan --serve                       # long-running Telegram listener
thegan --once "summarize the log"   # one turn, result pushed to THEGAN_CHAT
thegan --curate                      # fold staged memory into core.md

Model providers

Thegan talks to four vendors through one internal message format. Choose with THEGAN_PROVIDER (openrouter — the default — openai, anthropic or google) and set that vendor's API key. Tool calling, streaming replies and the status footer work identically for all four; the agent loop never has to know which vendor it is talking to.

THEGAN_PROVIDER=anthropic
ANTHROPIC_API_KEY=sk-ant-…
THEGAN_MODEL=claude-sonnet-4-6

Use the vendor's own model id in THEGAN_MODEL. Anthropic requires a token cap — THEGAN_MAX_TOKENS, default 4096. Only OpenRouter reports a per-request cost; for the others, set THEGAN_PRICE_IN/THEGAN_PRICE_OUT if you want the footer's cost figure.

Configuration

VariableReq.DefaultMeaning
TELEGRAM_TOKENyes—Bot token from @BotFather.
THEGAN_PROVIDERnoopenrouteropenrouter, openai, anthropic or google.
OPENROUTER_API_KEYyes—Key for the chosen provider (see above).
OPENAI_API_KEY / ANTHROPIC_API_KEY / GOOGLE_API_KEY——Set the one for your provider; Google also accepts GEMINI_API_KEY.
THEGAN_CHATyes*—The one chat allowed to use the bot (*unless THEGAN_ALLOW_ANY=1).
THEGAN_ALLOW_ANYno01 lets any chat use the bot — dangerous; skips the THEGAN_CHAT check.
THEGAN_MODELnodeepseek/deepseek-v4.1-flashMain model for the agent loop.
THEGAN_MAX_TOKENSno4096Token cap; Anthropic requires it, Gemini uses it when set.
THEGAN_UTILITY_MODELnogoogle/gemma-4-26b-a4b-itCheap model for live status summaries; empty disables them.
THEGAN_MAX_TURNSno50Hard cap on tool-call turns per message.
THEGAN_COMPACT_ATno100000Token threshold that triggers history compaction.
THEGAN_BASH_TIMEOUTno60Seconds before a bash tool call is killed.
THEGAN_TURN_TIMEOUTno300Seconds before a stuck turn is force-stopped.
THEGAN_STREAM_STALLno60Seconds before a stalled model stream is aborted.
THEGAN_MAX_AGEno600Telegram messages older than this many seconds are dropped.
THEGAN_VISIONnoonSend received images to the model as vision content; off sends only the saved path.
THEGAN_UPLOAD_DIRno$XDG_DATA_HOME/thegan/uploadsWhere received attachments are saved.
THEGAN_MAX_UPLOAD_MBno20Largest attachment to download, in MB (Telegram's bot limit).
TAVILY_API_KEYno—Tavily search key; enables the first web-search backend.
THEGAN_SEARCH_BACKENDSnotavily,searxng,duckduckgo,wikipediaOrder in which web-search backends are tried.
THEGAN_SEARCH_URLno—Base URL of a self-hosted SearxNG instance.
THEGAN_WEB_TIMEOUTno20Seconds per web request.
THEGAN_WEB_MAX_CHARSno20000Cap on the text web_fetch returns.
THEGAN_STATUSnoonSet to off to hide the status footer.
THEGAN_LOGnoinfoquiet, info or debug.
THEGAN_MEMORYnocore+logoff, core or core+log.
THEGAN_MEMORY_DIRno$XDG_DATA_HOME/theganWhere core.md and log.jsonl live.
THEGAN_CURATE_AUTOno10 stages core.candidate.md for /approve.

More options (timeouts, retries, memory limits, price fallbacks) are documented in contrib/env.example.

Chat commands

CommandWhat it does
/stopCancel the running turn immediately, even mid-turn.
/newStart a fresh session; durable facts are distilled in the background first. /new forget skips that.
/compactCompress the history into a summary now.
/memoryShow the curated profile and log statistics.
/remember <text>Append a durable fact to the log.
/forget <id|text>Remove matching log entries.
/promote <id>Stage a log entry for the curated profile.
/approve · /rejectApply or discard the candidate profile (only with THEGAN_CURATE_AUTO=0).

Built-in tools

bash, read, write, edit, grep, memory, web_search and web_fetch. Tool results are capped at roughly 2000 lines / 50 KB, and bash is killed after THEGAN_BASH_TIMEOUT seconds, so one huge command cannot flood the context.

Attachments

Send a photo, document, voice note, audio, video or sticker and Thegan fetches it with getFile, saves it under THEGAN_UPLOAD_DIR and appends the saved path to the prompt, so the agent can open it with its own tools. A caption, if present, becomes the question. Attachments larger than THEGAN_MAX_UPLOAD_MB (default 20 MB — Telegram's own bot limit) are refused with a short notice.

Images are additionally sent to the model as inline vision content. Set THEGAN_VISION=off to send only the path — useful for a text-only model or to avoid image tokens. If a model rejects image input, the turn is retried once automatically without the image.

Web search

Two tools reach the web. web_fetch downloads any http(s) URL and returns its readable text (HTML stripped of tags and scripts) — no key needed. web_search walks a configurable, ordered list of backends (THEGAN_SEARCH_BACKENDS, default tavily,searxng,duckduckgo,wikipedia) and returns the first that has results; a backend that is not configured is skipped.

The public search engines now sit behind JavaScript/bot walls, so Tavily or a self-hosted SearxNG is the reliable path; the two keyless backends are the fallback. web_fetch output is trimmed to THEGAN_WEB_MAX_CHARS.

Raspberry Pi

Thegan is designed for small ARM boards. The installer picks the matching prebuilt binary: armv7 for 32-bit Raspberry Pi OS, aarch64 for 64-bit. Supported: Pi 2, 3, 4, 5, Zero 2 W and 400.

curl -fsSL https://thegan.klauslochmann.com/install.sh | sh
mkdir -p ~/.config/thegan && $EDITOR ~/.config/thegan/env
loginctl enable-linger $USER      # start at boot without a login
systemctl --user enable --now thegan

No inbound port or public IP is needed: Thegan polls Telegram outbound. Measured on a Raspberry Pi 3 Model B (Debian 13): ~2 MB resident memory, 1.9 MB binary.

systemd

Run Thegan as a user service with the unit in contrib/thegan.service:

[Service]
EnvironmentFile=%h/.config/thegan/env
ExecStart=%h/.local/bin/thegan --serve
Restart=always

Scheduled jobs are plain systemd timers calling thegan --once. An hourly thegan --curate timer keeps the memory profile tidy; it exits before any model call unless something has been staged.

Security model

There is no tool permission layer: every tool is always enabled. Safety comes from the single allowed chat, dropping stale messages, capped tool output, and a system prompt that requires confirmation before state changes. Run Thegan on a machine you trust and do not expose it.

Read how memory works →