AI Assistant Python Code: Build a Useful Work Assistant

You can build a Python AI assistant in a few dozen lines using the OpenAI or Anthropic SDK. Here is a working skeleton, how to add tool access, and how to connect project context from fenbs over MCP.

Updated 4 min read

A Python AI assistant is a script or application that calls a language model API, passes the user's message, and returns the response. With a few additions — tool definitions, a message history, and an outside context source — it becomes a work assistant that can take action and remember what has been agreed. fenbs can supply that outside context over MCP, so the assistant reads the current project rules and notes at the start of every session.

The simplest version fits in under fifty lines. The useful version adds tools, a persistent message history, and a way to seed the conversation with the context the assistant needs for your specific work.

Libraries and setup

The two most common Python SDKs for AI assistants are the OpenAI SDK (openai) and the Anthropic SDK (anthropic). Both follow a similar pattern: create a client, pass a list of messages, and get a response back. Install the one you need:

Install the SDK
pip install openai
# or
pip install anthropic

Store your API key in an environment variable, not in the script itself. Use python-dotenv to load a .env file in development and set the variable directly in your deployment environment in production. Never commit an API key to version control.

A basic chat loop

The core of a Python AI assistant is a loop that reads a user message, sends it to the API with the conversation history, and prints the response. Here is a minimal example using the OpenAI SDK:

Minimal chat loop (OpenAI)
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
history = [{"role": "system", "content": "You are a helpful work assistant."}]

while True:
    user_input = input("You: ").strip()
    if not user_input:
        continue
    history.append({"role": "user", "content": user_input})
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=history
    )
    message = response.choices[0].message
    history.append({"role": "assistant", "content": message.content})
    print(f"Assistant: {message.content}")

This loop keeps the conversation history in memory. It resets when you restart the script. For a persistent assistant, save and load the history from a file or a database between sessions.

Adding tool access

Tools let the assistant take action: search the web, read a file, call an API. You define each tool as a function and a JSON schema that describes its parameters, then pass the tool definitions to the API. When the model decides to use a tool, it returns a tool call rather than a text response; your code runs the function and sends the result back.

A simple file-reading tool, for example, lets the assistant read a project notes file you maintain. A web-search tool lets it look up current information. Keep each tool focused on one job and validate the parameters your code receives from the model before using them. For advice on reviewing what AI-generated code does, see reviewing AI-generated code.

Persistent context between sessions

The biggest limitation of a basic chat loop is that it forgets everything when you restart. For a work assistant, that means re-explaining the project, re-stating the rules, and re-describing the current state of the work at the start of every session.

One approach is to save a context file and inject it into the system prompt at startup. You maintain the file manually — adding decisions, the current task, and notes — and the assistant reads it before answering. This works for a single person. For a team, a shared tool is more reliable than a file one person maintains.

Session-level context and project-level context serve different purposes. The conversation history records what was said in this session. Project context holds what was decided before it started: the goal, the rules, the current task state. Load project context into the system instruction or the first message at startup, then let the conversation history build from there. When a session ends, save the project-level conclusions back to the context source — decisions made, tasks completed, the next step. Saving the full conversation is rarely useful; saving what was learned and agreed is.

For a full-featured personal assistant that goes beyond a script, see AI personal assistant. For code generation tools that work in an IDE rather than a script, see AI code generators.

How fenbs helps

Give your Python assistant project context that persists across sessions

Connect your assistant to fenbs over MCP from Integrations. At the start of a session, the assistant reads the current project rules, where the work was left, and the decisions that apply. At the end of a session, it saves progress. The context is available the next time you run the script, from any machine, without maintaining a file manually.

Create a free account

Free for one person · No credit card · Works with Claude, ChatGPT, Cursor and more

How to build an AI assistant with fenbs context in Python

  1. Create a free account and add a project for the work your assistant will help with.
  2. Write the current tasks, decisions, and rules into fenbs. Those become the context your assistant reads.
  3. Connect an assistant to fenbs over MCP from Integrations. The integration guide walks through the setup for supported clients.
  4. In your Python script, add a startup step that calls the fenbs MCP tool to read the project context and injects it into the system prompt or the first user message.
  5. At the end of a session, save progress back to fenbs: what was completed, what changed, what the next step is.

The MCP connection requires a client that supports it. If you are building a custom Python loop, you can implement MCP tool calls using the standard JSON-RPC protocol. See fenbs Integrations for the supported clients and their setup guides. For a comparison of AI coding assistants that work inside an IDE, see JetBrains AI assistant.

fenbs does not train a model or store chat history. It stores the work record: projects, tasks, decisions, and notes. Your Python script reads that record and uses it as context. The conversation itself still lives in your script's history variable.

Questions people ask.

Which Python library should I use to build an AI assistant?

The OpenAI SDK and the Anthropic SDK are the most widely used. Both follow a similar message-based pattern. The right choice depends on which model you want to use. You can also use a provider-agnostic library like LangChain if you want to switch models without rewriting the integration code.

How do I give my Python AI assistant persistent memory?

Save and load the conversation history between sessions. For project context that outlasts individual conversations — decisions, rules, current task state — keep a separate record and inject it into the system prompt at startup. A shared tool like fenbs keeps that record maintainable without requiring one person to update a file.

How do I add voice to a Python AI assistant?

Use a speech-to-text library like OpenAI Whisper to convert audio input to text, then pass that text to the language model. For the response, use a text-to-speech library or the OpenAI TTS API to convert the text back to audio. The core chat loop stays the same.

Is it safe to give a Python AI assistant tool access?

With care. Validate the parameters the model sends before executing any tool function. Limit what each tool can do — a file-reading tool should not be able to write files, for example. Review what your assistant did each session, especially early on. Start with read-only tools and add write access only when you trust the behavior.

What is MCP and do I need it for a Python assistant?

MCP is the Model Context Protocol, a standard way for an AI app to call an outside tool. You do not need it for a basic Python assistant. It becomes useful when you want your assistant to read from a shared record like fenbs without maintaining that record yourself. Supported clients handle the connection; you configure it once.

More in Connect your AI tools

All Connect your AI tools articles

Start with one thing.

Free for one person · No credit card · Works with Claude, ChatGPT, Cursor and more

There is nothing to set up first. Write one line and you’ve started.