Skip to main content

thinai

Python SDK for Thinai, the Android app that runs LLMs offline on your phone and serves them to your Wi-Fi.

Point your laptop at the phone and use its models from Python. No cloud inference: the model runs on the phone, and on your Wi-Fi your data stays on your network.

pip install thinai
from thinai import Thinai

client = Thinai(api_key="<token from the Server page>")  # finds the phone on your Wi-Fi
print(client.chat("Explain RAG in one sentence.").content)

Works with Thinai app 2.6 or later. Older versions work too, minus health() and the Public URL.

Set up the phone

  1. Open Thinai, download a model, and go to the Server tab.
  2. Press Start.
  3. Turn on Share on local network.
  4. Under API tokens, create a token for this computer and copy it. Every LAN or Public URL request needs one; deleting the token cuts that client off at once.
  5. Keep the computer on the same Wi-Fi. The app shows the address, for example http://192.168.1.36:11434.

To reach the phone from outside your Wi-Fi, turn on Public URL (Beta) instead. The app opens a free Cloudflare quick tunnel and shows an https://….trycloudflare.com address. Requests and replies pass through Cloudflare, and the address changes when you rotate it or restart the tunnel.

Connecting

from thinai import Thinai, discover

client = Thinai()                           # scan the local /24 for Thinai
client = Thinai("192.168.1.36")             # known IP
client = Thinai("192.168.1.36", port=8080)  # custom port from the app
client = Thinai("http://192.168.1.36:11434", model="gemma-3-270m-it-q8_0")
client = Thinai("https://abc-xyz.trycloudflare.com", api_key=token)  # Public URL

client.health()                             # True if the server is up (GET /healthz)

for server in discover(api_key=token):      # every phone on the network
    print(server.base_url, server.models, server.auth_required)

The token goes in api_key= or $THINAI_API_KEY, and is sent as Authorization: Bearer <token>. Apps on the phone itself calling 127.0.0.1 don't need one. A missing or wrong token raises AuthenticationError.

https:// URLs without a port use 443, which is what the Public URL needs; plain hosts and http:// URLs get port 11434. A bare *.trycloudflare.com host is treated as https.

When you don't pass host, the client looks in this order:

  1. $THINAI_HOST
  2. a Thinai app on this machine
  3. a scan of your local subnet

The app doesn't advertise itself on the network yet. The scan probes GET / on each address and matches the Thinai is running reply, or the app's 401 with WWW-Authenticate: Bearer realm="thinai" when the server needs a token. It usually takes 1–2 seconds. Finding a server doesn't need a token; listing its models does. Pass discover(subnets=["10.0.0.0/24"]) if your network isn't a /24.

Chat

from thinai import Message

response = client.chat(
    [Message.system("You are concise."), Message.user("What is a GGUF file?")],
    temperature=0.3,
    top_k=40,
    num_ctx=4096,      # clamped by the phone to client.show().context_cap
    num_predict=256,
    think=False,       # turn a thinking model's reasoning off
)
print(response.content, response.done_reason)
print(f"{response.metrics.tokens_per_second:.1f} tok/s on the phone")

To stream the reply as it's generated:

for chunk in client.chat("Write a haiku about the monsoon.", stream=True):
    print(chunk.content, end="", flush=True)

If you leave out model, the client uses the model already loaded on the phone. Swapping models on a phone is slow. Sampling values you don't pass fall back to the model's settings in the app.

Images work with a vision model whose image encoder is loaded on the phone (for example Gemma 3 4B). Pass file paths, bytes or data:image/... URIs; the SDK inlines them, since the phone never fetches remote URLs:

client.chat("What is in this photo?", images=["receipt.jpg"])
client.chat([Message.user("Compare these.", images=[png_bytes, "b.png"])])

Without a vision model loaded, the phone answers BadRequestError.

Tool calling uses OpenAI-format tool definitions:

tools = [{"type": "function", "function": {
    "name": "get_weather",
    "parameters": {"type": "object", "properties": {"city": {"type": "string"}}},
}}]
response = client.chat("Weather in Chennai?", tools=tools)
for call in response.tool_calls:
    print(call.name, call.arguments)

Generate, embed, models

client.generate("Once upon a time", num_predict=50).response
client.embed(["hello", "world"], model="<embedding-model-id>").embeddings  # needs an embedding model
client.models()          # installed models
client.running()         # loaded model
client.show()            # architecture, trained context_length, context_cap

Async

import asyncio
from thinai import AsyncThinai

async def main():
    async with AsyncThinai() as client:
        async for chunk in await client.chat("Hi!", stream=True):
            print(chunk.content, end="")

asyncio.run(main())

OpenAI-compatible API

The phone also serves /v1/chat/completions (streaming, tools, tool_choice, images, GBNF grammar), /v1/embeddings (encoding_format, dimensions) and /v1/models. To use them through the official client, which gets the client's token as its API key:

pip install 'thinai[openai]'
oai = Thinai().openai()
oai.chat.completions.create(model="lfm2.5-350m-q8_0", messages=[{"role": "user", "content": "hi"}])

This means LangChain, LlamaIndex and other OpenAI-compatible tools work too. Give them client.base_url + "/v1" as the base URL and your token as the API key.

Command line

thinai discover
thinai health --host https://abc-xyz.trycloudflare.com
thinai models --host 192.168.1.36 --api-key "$TOKEN"
thinai chat "What is the capital of Tamil Nadu?"   # token from $THINAI_API_KEY

Errors

Exception When
DiscoveryError No phone found on the network
APIConnectionError / APITimeoutError Phone unreachable (server stopped, sharing off, different Wi-Fi, tunnel closed)
AuthenticationError Missing, wrong or deleted API token
NotFoundError Unknown model id
BadRequestError Invalid request, such as embedding with a chat model, or an image without a vision model
ServerError Inference failed on the phone
StreamError Server reported an error mid-stream

All of them inherit from thinai.ThinaiError.

Good to know

  • Tokens protect LAN and Public URL access. Create one token per client in the app and delete it to revoke that client. GET /healthz is the only route open without one, and it returns no data. Traffic on the LAN is plain HTTP, so only share on networks you trust.
  • The Public URL goes through Cloudflare. It is https end to end to Cloudflare's edge, but Cloudflare sees requests and replies. Use it only for prompts you're comfortable with that.
  • Security issues. Please report them privately. See SECURITY.md.
  • One request at a time. The phone processes requests in a queue, so concurrent calls wait. Cancelling a request on the client doesn't stop generation on the phone.
  • Engine failures are errors. Since app 2.5, "request exceeds the available context size" and similar failures come back as ServerError (or StreamError mid-stream), not as reply text.
  • num_ctx only works on /api/* routes. The OpenAI-compatible /v1/chat/completions always uses the context size set in the app.

Development

uv sync
uv run pytest                                    # unit tests (offline)
THINAI_HOST=192.168.1.36 uv run pytest -m live   # against a real phone
uv run ruff check . && uv run mypy
uv build && uv run twine check dist/*

License

MIT

Metadata

Release files for thinai 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for thinai 0.2.0
File Size Uploaded Provenance Actions
thinai-0.2.0.tar.gz 29.1 kB Without provenance Details for thinai-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for thinai 0.2.0
File Interpreter ABI Platform Actions
thinai-0.2.0-py3-none-any.whl Python 3 none any Details for thinai-0.2.0-py3-none-any.whl

Total release size: 55.3 kB

Release files / thinai-0.2.0.tar.gz

Download URL thinai-0.2.0.tar.gz
Size 29.1 kB
Tags Source
SHA-256 checksum
How to use checksums
d797d731040396d37637fc9f1305dcb58dc9cfc342e1514069e091c4e53373ad
BLAKE2b-256 checksum
How to use checksums
40b7293d35d41ac9825bc8b8defd3c8f31b97768c4d90545f1db4ef6f661f5ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Release files / thinai-0.2.0-py3-none-any.whl

Download URL thinai-0.2.0-py3-none-any.whl
Size 26.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0c3e1751163cd31dabea8e92dec2ed5b1ae922c2447b0c3aaf2de0a1c9af9a53
BLAKE2b-256 checksum
How to use checksums
939340804e63277d70c15c660284cc9993c8553431d330fbb9c7349142848f40
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.7

Security for thinai 0.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page