# How to Build an AI Assistant in Termux With Python

> Source: <https://dev.to/terminaltools/how-to-build-an-ai-assistant-in-termux-with-python-4278>
> Published: 2026-09-17 18:46:54+00:00

You can build a simple AI assistant directly from your Android phone using [Termux](https://terminaltools.blogspot.com/p/termux-tutorial-comprehensive-guide-to.html) 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](https://terminaltools.blogspot.com/2025/09/how-to-install-and-use-python-in-termux.html):

```
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](https://terminaltools.blogspot.com/2025/08/cyber-security-plan-for-small-business.html) is a useful next read.

Now create the Python file:

```
nano assistant.py
```

Add this code:

``` python
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](https://terminaltools.blogspot.com/2026/09/run-ai-coding-agents-on-termux-2026.html), or the general project ideas in [3 easy Termux projects you can build on Android](https://terminaltools.blogspot.com/2025/07/quick-termux-projects-you-can-do.html).

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?
