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
| Variable | Req. | Default | Meaning |
|---|---|---|---|
TELEGRAM_TOKEN | yes | — | Bot token from @BotFather. |
THEGAN_PROVIDER | no | openrouter | openrouter, openai, anthropic or google. |
OPENROUTER_API_KEY | yes | — | 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_CHAT | yes* | — | The one chat allowed to use the bot (*unless THEGAN_ALLOW_ANY=1). |
THEGAN_ALLOW_ANY | no | 0 | 1 lets any chat use the bot — dangerous; skips the THEGAN_CHAT check. |
THEGAN_MODEL | no | deepseek/deepseek-v4.1-flash | Main model for the agent loop. |
THEGAN_MAX_TOKENS | no | 4096 | Token cap; Anthropic requires it, Gemini uses it when set. |
THEGAN_UTILITY_MODEL | no | google/gemma-4-26b-a4b-it | Cheap model for live status summaries; empty disables them. |
THEGAN_MAX_TURNS | no | 50 | Hard cap on tool-call turns per message. |
THEGAN_COMPACT_AT | no | 100000 | Token threshold that triggers history compaction. |
THEGAN_BASH_TIMEOUT | no | 60 | Seconds before a bash tool call is killed. |
THEGAN_TURN_TIMEOUT | no | 300 | Seconds before a stuck turn is force-stopped. |
THEGAN_STREAM_STALL | no | 60 | Seconds before a stalled model stream is aborted. |
THEGAN_MAX_AGE | no | 600 | Telegram messages older than this many seconds are dropped. |
THEGAN_VISION | no | on | Send received images to the model as vision content; off sends only the saved path. |
THEGAN_UPLOAD_DIR | no | $XDG_DATA_HOME/thegan/uploads | Where received attachments are saved. |
THEGAN_MAX_UPLOAD_MB | no | 20 | Largest attachment to download, in MB (Telegram's bot limit). |
TAVILY_API_KEY | no | — | Tavily search key; enables the first web-search backend. |
THEGAN_SEARCH_BACKENDS | no | tavily,searxng,duckduckgo,wikipedia | Order in which web-search backends are tried. |
THEGAN_SEARCH_URL | no | — | Base URL of a self-hosted SearxNG instance. |
THEGAN_WEB_TIMEOUT | no | 20 | Seconds per web request. |
THEGAN_WEB_MAX_CHARS | no | 20000 | Cap on the text web_fetch returns. |
THEGAN_STATUS | no | on | Set to off to hide the status footer. |
THEGAN_LOG | no | info | quiet, info or debug. |
THEGAN_MEMORY | no | core+log | off, core or core+log. |
THEGAN_MEMORY_DIR | no | $XDG_DATA_HOME/thegan | Where core.md and log.jsonl live. |
THEGAN_CURATE_AUTO | no | 1 | 0 stages core.candidate.md for /approve. |
More options (timeouts, retries, memory limits, price fallbacks) are documented in contrib/env.example.
Chat commands
| Command | What it does |
|---|---|
/stop | Cancel the running turn immediately, even mid-turn. |
/new | Start a fresh session; durable facts are distilled in the background first. /new forget skips that. |
/compact | Compress the history into a summary now. |
/memory | Show 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 · /reject | Apply 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.
- Tavily — a real web search; set
TAVILY_API_KEY. - SearxNG — your own instance via
THEGAN_SEARCH_URL(e.g.http://127.0.0.1:8080), queried withformat=json. - DuckDuckGo Instant Answer and Wikipedia — no key, so search still works with nothing configured.
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.