cd /news/ai-tools/how-to-build-an-ai-assistant-in-term… · home topics ai-tools article
[ARTICLE · art-132904] src=dev.to ↗ pub= topic=ai-tools verified=true sentiment=· neutral

How to Build an AI Assistant in Termux With Python

A developer published a step-by-step guide for building a simple AI assistant on an Android phone using Termux and Python. The tutorial walks through installing Python and the OpenAI package, storing an API key in a .env file with python-dotenv, and running a terminal loop that sends user questions to the gpt-5-mini model via the Responses API. The guide notes that forcing a plain text output format prevents the model from returning only internal reasoning and leaving output_text empty.

by read4 min views1 publishedSep 17, 2026

You can build a simple AI assistant directly from your Android phone using Termux and Python.

You do not need a computer or a complicated development setup. With a Python script, an AI API, and a few packages, you can create an assistant that accepts your questions and returns AI-generated responses from the terminal.

In this guide, we will build a simple version from scratch and run it inside Termux.

Before starting, make sure you have:

We will use Python to handle the conversation and send requests to the AI API.

First, update Termux packages:

pkg update && pkg upgrade

Then install Python. If you want a deeper walkthrough of this step, see our guide on how to install and use Python in Termux for beginners:

pkg install python

Check that it was installed correctly:

python --version

You should see the installed Python version.

Next, create a directory for the project:

mkdir ai-assistant
cd ai-assistant

For this example, we can use the OpenAI Python package to communicate with the API.

Install it with:

pip install openai

We also need a way to keep the API key outside the Python code. Install python-dotenv:

pip install python-dotenv

This keeps your credentials separate from the main script.

Create a .env file:

nano .env

Add your API key:

OPENAI_API_KEY=your_api_key_here

Replace your_api_key_here with your actual API key.

Save the file and exit nano.

Do not share this file publicly or upload it to GitHub. An exposed API key can be used by someone else and may result in unexpected API usage. If you want to go further and build good security habits around credentials in general, our guide on building a cyber security plan for a small project or business is a useful next read.

Now create the Python file:

nano assistant.py

Add this code:

import os
from dotenv import load_dotenv
from openai import OpenAI

load_dotenv()

client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

print("AI Assistant")
print("Type 'exit' to quit.\n")

while True:
    question = input("You: ")

    if question.lower() == "exit":
        print("Goodbye!")
        break

    try:
        response = client.responses.create(
            model="gpt-5-mini",
            input=question,
            text={"format": {"type": "text"}}
        )

        print(f"AI: {response.output_text}\n")

    except Exception as e:
        print(f"Error: {e}\n")

The script does a few simple things.

It loads the API key from the .env file, creates an API client, waits for your input, sends the question to the AI model, and prints the response.

The text={"format": {"type": "text"}} argument is worth calling out: with the Responses API, gpt-5-mini will sometimes return only internal reasoning and leave output_text empty. Forcing a plain text output format avoids that and ensures you consistently get a visible reply.

The while loop keeps the assistant running until you type:

exit

Start the program with:

python assistant.py

You should see something similar to:

AI Assistant
Type 'exit' to quit.

You:

You can now type a question:

You: Explain Python functions in simple terms

The assistant sends the question to the AI model and displays the response in Termux.

You can continue asking questions without restarting the program.

The basic assistant is intentionally simple, but you can build on it.

For example, you could add conversation history so the assistant remembers previous messages during the current session.

You could also add commands for specific tasks, such as:

/help
/clear
/exit

Another useful addition would be voice input and text-to-speech, turning the terminal program into something closer to a voice assistant.

You could even connect it to other Termux features so the assistant can work with files, run approved commands, or automate tasks — similar in spirit to what we covered in how to run AI coding agents on Termux in 2026, or the general project ideas in 3 easy Termux projects you can build on Android.

The important part is that Termux is not limited to running simple Python scripts. It can provide a useful environment for experimenting with AI applications directly from Android.

Building an AI assistant in Termux does not require a large project. A small Python script and an AI API are enough to get started.

Once the basic version works, you can gradually add memory, commands, voice features, file handling, and other capabilities.

What would you add to your own Termux AI assistant first?

── more in #ai-tools 4 stories · sorted by recency
── more on @termux 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/how-to-build-an-ai-a…] indexed:0 read:4min 2026-09-17 ·