Termux-AIChain
_____ ___ _____ _____ _ _
|_ _| / _ \|_ _/ __ \ | (_)
| | ___ _ __ _ __ ___ _ ___ __ / /_\ \ | | | / \/ |__ __ _ _ _ __
| |/ _ \ '__| '_ ` _ \| | | \ \/ / ___ | _ | | | | | | '_ \ / _` | | '_ \
| | __/ | | | | | | | |_| |> < |___|| | | |_| |_| \__/\ | | | (_| | | | | |
\_/\___|_| |_| |_| |_|\__,_/_/\_\ \_| |_/\___/ \____/_| |_|\__,_|_|_| |_|
Sovereign Zero-Dependency AI Chaining & Autonomous Agent Framework for Android Termux
Dual-Engine Architecture (Pure Python 3.10+ Stdlib & Pure Node.js 18+ ESM) with Native ARM64 Acceleration & 0 External Dependencies
Official Documentation • Why Not LangChain? • Python Quickstart • Node.js ESM Quickstart • LCEL Pipeline Recipes • Hardware Actuation • Tuning Parameters
🚀 Why termux-aichain Instead of LangChain on Termux & Edge?
Running standard desktop AI frameworks like LangChain, LlamaIndex, or CrewAI inside an Android Termux (Bionic libc / ARM64) environment is notoriously fragile, resource-exhausting, and often impossible.
termux-aichain is purpose-built from ground zero to replace bloated desktop frameworks with an ultra-lightweight, zero-external-dependency, edge-native engine that delivers full LCEL pipe compatibility (prompt | llm | parser), StateGraph multi-agent execution, and native Android smartphone actuation.
Comprehensive Head-to-Head Comparison
| Architectural Property | LangChain (Standard / Heavy) | termux-aichain (Edge-Native SSOT) |
Engineering Impact on Termux |
|---|---|---|---|
| External Dependencies | 45 ~ 85 heavy packages (Pydantic, SQLAlchemy, aiohttp, requests, tenacity, dataclasses-json) |
0 external dependencies (100% Python Standard Library: urllib, json, sqlite3, subprocess) |
Zero dependency hell. Installs in <1 second with pip install --no-deps. |
| Package Disk Footprint | 180 MB ~ 450 MB (after installing all transitive dependencies) |
94 KB (wheel) 268 KB (installed unpacked) |
99.9% storage reduction. Ideal for storage-constrained mobile flash storage. |
| Cold Start Import Time | 3,200 ms ~ 7,500 ms (heavy module discovery & metaclass loading) |
12.8 ms | 250x faster startup. Instant CLI execution without perceptible lag. |
| Baseline RAM Footprint (RSS) | 150 MB ~ 240 MB (idle baseline before running inference) |
14.2 MB | Prevents Android LMK (Low Memory Killer) crashes. Leaves 98% of RAM for the LLM weights. |
| Android Bionic C/Rust Compilation | Frequently Fails (Pydantic-core, Chroma, Tokenizers, Tiktoken fail on Android ARM64) |
Zero Compilation Needed (Pure Python stdlib & Pure Node.js ESM) |
Runs out-of-the-box. No clang, rustc, or build tools required in Termux. |
| Mobile Hardware Actuation | None (0 built-in tools) (Designed purely for cloud APIs and server containers) |
Built-in First-Class Tools (Battery, Temperature, GPS, Vibration, Camera, TTS, Notifications, Shell) |
Direct hardware actuation. The AI directly inspects and controls the physical phone. |
| Vector Store & RAG Engine | Requires heavy C++ stores (ChromaDB, FAISS, Pinecone) |
Built-in SQLite Vector Store (In-memory or .db, pure math cosine similarity + FTS5) |
Zero external vector DB needed. Store embeddings directly in mobile SQLite. |
| **LCEL Pipe (` | `) Syntax** | Supported (`prompt | llm |
| Dual Runtime Support | Separate fragmented libraries | 100% API Parity between Python & Node.js ESM | Seamlessly build agent workflows in Python or TypeScript/ESM. |
5 Fatal Flaws of Running LangChain on Mobile (and How termux-aichain Solves Them)
-
The Native Wheel & Rust Compilation Barrier: LangChain requires
pydantic-core,chromadb, andtiktoken. On Android Termux, prebuilt wheels are rarely published for the Android Bionic C runtime. Users are forced to spend hours installingclang,binutils, andrustc, which frequently crash due to memory exhaustion during compilation.termux-aichainis written in 100% pure Python standard library and Node.js standard modules. No compiler is ever invoked. -
Android Low Memory Killer (LMK) Aggression: When loading a 1B~3B LLM (e.g., Llama-3.2 1B occupying ~800MB RAM), the phone's memory margin is thin. LangChain's idle overhead of 180MB+ pushes total process memory past the Android foreground limit, triggering instant
SIGKILLby the kernel.termux-aichainhas a baseline RSS footprint of only 14.2MB, granting virtually the entire memory budget to model weights. -
Subprocess & Mobile API Impedance Mismatch: Desktop agent frameworks expect cloud API keys (OpenAI, Anthropic) or desktop Docker daemons. They have zero awareness of mobile power management, Android battery states, or thermal throttling.
termux-aichainprovides hardware-native sensor tools (get_battery_status,get_sensor_data,vibrate_device,send_notification) as native first-class functions. -
Import Latency in Ephemeral CLI Runs: Running a command-line utility with LangChain requires waiting 3 to 7 seconds just for Python to finish importing modules before the first line of code executes.
termux-aichainimports in 12.8ms using lazy import tables. -
Serverless Vector RAG Without Heavy External Engines: Setting up FAISS or ChromaDB in Termux requires C++ compilers and large shared libraries.
termux-aichainincludesSQLiteVectorStore, utilizing the standard librarysqlite3and pure-math cosine similarity.
🐍 Python Quickstart
Installation
# In Termux or Linux (Python 3.10+):
pip install --upgrade termux-aichain
1-Line Hello Agent (Local llama-server / OpenAI-Compatible)
from termux_aichain import LocalAgent
# Connects directly to local llama-server (127.0.0.1:8080) or BitNet
agent = LocalAgent.local(model="llama3")
response = agent.run("Hello! Introduce yourself in one short sentence.")
print(response)
🟩 Node.js / TypeScript Quickstart
Installation
# In Termux or Node.js (v18+ ESM):
npm install termux-aichain
1-Line Hello Agent (ESM)
import { LocalAgent } from "termux-aichain";
// Connects directly to local llama-server (127.0.0.1:8080)
const agent = await LocalAgent.local("llama3");
const response = await agent.run("Hello! Introduce yourself in one short sentence.");
console.log(response);
🧩 LCEL Pipeline Recipes (LangChain Expression Language)
termux-aichain implements the full Runnable Protocol, allowing arbitrary chaining using the pipe operator (|).
Recipe 1: PromptTemplate | Model | StringOutputParser
from termux_aichain import PromptTemplate, OpenAICompatibleChat, StringOutputParser
# 1. Define prompt with variables
prompt = PromptTemplate.from_template(
"You are an on-device AI running on Android. Explain {concept} in one crisp sentence."
)
# 2. Bind to local inference server (port 8080)
llm = OpenAICompatibleChat(
base_url="http://127.0.0.1:8080/v1",
model="llama3",
temperature=0.3,
max_tokens=64
)
# 3. Assemble LCEL chain
chain = prompt | llm | StringOutputParser()
# 4. Invoke synchronously
result = chain.invoke({"concept": "edge computing"})
print("Result:", result)
Recipe 2: Streaming Token Generation with Real-Time Telemetry
from termux_aichain import OpenAICompatibleChat, HumanMessage, SystemMessage
llm = OpenAICompatibleChat(base_url="http://127.0.0.1:8080/v1", model="llama3")
messages = [
SystemMessage(content="You are a sovereign mobile assistant."),
HumanMessage(content="Write a 3-bullet summary of edge AI benefits.")
]
# Real-time token streaming with zero memory accumulation
for chunk in llm.stream(messages):
print(chunk.content, end="", flush=True)
print()
🤖 Autonomous ReAct Agent with Android Hardware Actuation
Agents can observe real physical hardware metrics (battery percentage, device temperature, charging status) and actuate physical outputs (vibrate, notification, sound).
import json
from termux_aichain import (
create_react_agent,
OpenAICompatibleChat,
HumanMessage,
get_battery_status,
vibrate_device,
send_notification
)
# 1. Initialize local LLM
llm = OpenAICompatibleChat(base_url="http://127.0.0.1:8080/v1", model="llama3")
# 2. Create ReAct agent with native Android hardware tools
agent = create_react_agent(
model=llm,
tools=[get_battery_status, vibrate_device, send_notification],
system_prompt="You are an autonomous smartphone agent. Inspect device sensors and take action when requested."
)
# 3. Execute reasoning-and-acting loop
result = agent.invoke({
"messages": [
HumanMessage(content="Check my phone battery. If temperature is below 35C, vibrate the device for 300ms.")
]
})
print("Agent Diagnostic:", result["messages"][-1].content)
🗄️ Zero-Dependency On-Device SQLite Vector Store (RAG)
Store embeddings and perform semantic cosine similarity search directly inside mobile SQLite without installing ChromaDB, FAISS, or C++ dependencies.
from termux_aichain import SQLiteVectorStore
# 1. Initialize vector store in memory or local file ('knowledge.db')
vector_store = SQLiteVectorStore(":memory:")
# 2. Add documents with pre-computed or local embeddings
documents = [
"Galaxy S25 acts as the Master Coordinator running llama-server on port 8080.",
"Galaxy A53 acts as the Dedicated Speech Worker running neural TTS on port 8088.",
"The AMEVA ecosystem executes sovereign edge inference with zero cloud egress."
]
# 4-dimensional sample vectors
vectors = [
[1.0, 0.2, 0.0, 0.0],
[0.0, 1.0, 0.2, 0.0],
[0.1, 0.1, 1.0, 0.0]
]
vector_store.add_texts(documents, vectors, metadatas=[{"node": "s25"}, {"node": "a53"}, {"node": "ameva"}])
# 3. Query by vector (Finds Speech Worker document with highest cosine similarity)
query_vec = [0.0, 0.95, 0.1, 0.0]
matches = vector_store.similarity_search_by_vector(query_vec, k=1)
print("Retrieved Document:", matches[0].page_content)
# Output: "Galaxy A53 acts as the Dedicated Speech Worker running neural TTS on port 8088."
📱 Android Hardware Actuation Tools (First-Class Citizens)
All tools run with strict JSON schema validation, timeout protection, and fail-safe fallbacks.
| Tool Function | Description | Parameter Example | Return Telemetry |
|---|---|---|---|
get_battery_status() |
Probes Android kernel battery & thermal state | None | {"percentage": 48, "temperature": 29.4, "status": "DISCHARGING"} |
get_sensor_data(sensor) |
Reads physical hardware sensors | {"sensor": "accelerometer"} |
{"values": [0.12, 9.81, 0.05], "timestamp": 172824...} |
get_device_location(provider) |
Fetches GPS/Network coordinates | {"provider": "gps"} |
{"latitude": 37.5665, "longitude": 126.9780, "accuracy": 12.0} |
vibrate_device(duration_ms) |
Triggers phone haptic vibration | {"duration_ms": 300} |
{"vibrated": true, "duration_ms": 300} |
send_notification(title, content) |
Posts an Android system notification | {"title": "Alert", "content": "Done"} |
{"posted": true, "id": 101} |
record_speech_to_text(duration_sec) |
Records microphone & returns audio text | {"duration_sec": 5} |
{"transcript": "Hello world", "confidence": 0.94} |
execute_shell(command, timeout_sec) |
Safely runs sandboxed Termux commands | {"command": "uptime"} |
{"exit_code": 0, "stdout": "...", "stderr": ""} |
🛠️ Hardware Tuning & Sampling Parameters
Local Server Configuration (LocalServerConfig)
from termux_aichain import LocalServerConfig, LocalServerManager
config = LocalServerConfig(
model_path="/data/data/com.termux/files/home/models/llama-3.2-1b.gguf",
threads=8, # CPU computation threads (Oryon / Cortex-X)
n_ctx=2048, # Context window length
n_batch=512, # Prompt evaluation batch size
n_ubatch=256, # Micro-batch size for constrained mobile RAM
n_gpu_layers=0, # 0 for CPU, 99 for Adreno/Mali GPU offload
port=8080, # HTTP listening port
flash_attn=False, # Flash Attention toggle
cache_type_k="f16", # Key cache quantization ("f16", "q8_0", "q4_0")
cache_type_v="f16", # Value cache quantization
mlock=False # Lock model memory to prevent disk thrashing
)
Sampling Parameters (OpenAICompatibleChat / BitNetChat)
| Parameter | Type | Default | Valid Range | Technical Function |
|---|---|---|---|---|
temperature |
float |
0.7 |
0.0 ~ 2.0 |
Randomness control (0.0 for deterministic JSON/Code). |
top_p |
float |
0.95 |
0.0 ~ 1.0 |
Nucleus cumulative probability cutoff. |
top_k |
int |
40 |
1 ~ 100 |
Top-K candidate pool token limit. |
min_p |
float |
0.05 |
0.0 ~ 1.0 |
Minimum probability cutoff against hallucinations. |
repeat_penalty |
float |
1.18 |
1.0 ~ 2.0 |
Frequency penalty scale to avoid repetition loops. |
max_tokens |
int |
128 |
1 ~ 4096 |
Upper limit on generated tokens. |
stop |
List[str] |
None |
List[str] |
Stop tokens list, e.g. `["< |
timeout |
float |
20.0 |
1.0 ~ 300.0 |
HTTP socket timeout in seconds. |
📊 Physical Real-Device Benchmarks (Galaxy S25 & A53)
Measured on actual hardware (Samsung Galaxy S25 Snapdragon 8 Elite / Galaxy A53 Exynos 1280):
| Metric | LangChain (Standard) | termux-aichain v1.1.4 |
Delta |
|---|---|---|---|
| Cold Start Import Time | 3,840.0 ms | 12.8 ms | 300x Faster |
| Idle Memory Footprint (RSS) | 185.0 MB | 14.2 MB | 92.3% Less RAM |
| Package Disk Size | 210.0 MB | 0.09 MB (94 KB) | 99.9% Smaller |
| External Dependencies | 48 packages | 0 packages | Zero External Deps |
| Android Termux Installation | Often Fails (Rust/Bionic) | Instant (<1 sec) | Zero Failures |
| Time to First Token (TTFT) | ~950 ms | 827 ms | 13% Faster |
| Hardware Actuation | Not Supported | Native (Battery, Sensors, Haptic) | Fully Supported |
| Automated Test Suite | N/A | 169 / 169 PASS | 100% Validated |
🔒 Security Architecture: Default-Deny Tool Policy
termux-aichain implements strict zero-trust security for mobile hardware actuation:
from termux_aichain import ToolPolicy, ToolRule
# Default-deny security policy
policy = ToolPolicy(
default="deny",
rules=[
ToolRule(name="get_battery_status", allow=True),
ToolRule(name="vibrate_device", allow=True, max_calls_per_minute=10),
ToolRule(name="execute_shell", allow=False) # Explicitly forbidden
]
)
📜 Official Ecosystem & Links
- Official Web Documentation: https://uno-km.vercel.app/lib/aichain/
- PyPI Registry: https://pypi.org/project/termux-aichain/
- npm Registry: https://www.npmjs.com/package/termux-aichain
- GitHub Repository: https://github.com/uno-km/termux-aichain
- AMEVA Open-Source Foundation (AOSF)
📄 License
Licensed under the Apache License, Version 2.0 (Apache-2.0). Copyright (c) 2026 Eunho Kim (@uno-km).
Metadata
Release files for termux-aichain 1.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| termux_aichain-1.1.5.tar.gz | 115.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| termux_aichain-1.1.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 211.5 kB
Release files / termux_aichain-1.1.5.tar.gz
| Download URL | termux_aichain-1.1.5.tar.gz |
|---|---|
| Size | 115.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
bd8529f3b1a929819b269480d7cfb043bc66e923ca44bd2fcc9f995be9a94d3e
|
|
BLAKE2b-256 checksum How to use checksums |
67e841da166fc5957b897bf75e94b1412598babbaef4efcbc40c99d3f47825ec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.0
|
Release files / termux_aichain-1.1.5-py3-none-any.whl
| Download URL | termux_aichain-1.1.5-py3-none-any.whl |
|---|---|
| Size | 96.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
903c0c09f7724b8896482c43d5ea39954e2fed872d386e411eeb0fdf758f79cc
|
|
BLAKE2b-256 checksum How to use checksums |
59a431b41d3ba259ee75ac2a560dd83afa095dd4fae1f5e4caf304549dc3b1b4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.12.0
|