DEV Community

Cover image for How to Build an AI Assistant in Termux With Python
Stephano Kambeta
Stephano Kambeta

Posted on

How to Build an AI Assistant in Termux With Python

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.

What You Need

Before starting, make sure you have:

  • Termux installed
  • Basic knowledge of using the terminal
  • Python installed
  • An API key from an AI provider

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

Set Up Python in Termux

First, update Termux packages:

pkg update && pkg upgrade
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Check that it was installed correctly:

python --version
Enter fullscreen mode Exit fullscreen mode

You should see the installed Python version.

Next, create a directory for the project:

mkdir ai-assistant
cd ai-assistant
Enter fullscreen mode Exit fullscreen mode

Install the Python Package

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

Install it with:

pip install openai
Enter fullscreen mode Exit fullscreen mode

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

pip install python-dotenv
Enter fullscreen mode Exit fullscreen mode

This keeps your credentials separate from the main script.

Add Your API Key

Create a .env file:

nano .env
Enter fullscreen mode Exit fullscreen mode

Add your API key:

OPENAI_API_KEY=your_api_key_here
Enter fullscreen mode Exit fullscreen mode

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.

Create the Assistant

Now create the Python file:

nano assistant.py
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Run the Assistant

Start the program with:

python assistant.py
Enter fullscreen mode Exit fullscreen mode

You should see something similar to:

AI Assistant
Type 'exit' to quit.

You:
Enter fullscreen mode Exit fullscreen mode

You can now type a question:

You: Explain Python functions in simple terms
Enter fullscreen mode Exit fullscreen mode

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

You can continue asking questions without restarting the program.

Make It More Useful

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
Enter fullscreen mode Exit fullscreen mode

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.

Final Thoughts

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?

Top comments (0)