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:
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:
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.
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 accountFree for one person · No credit card · Works with Claude, ChatGPT, Cursor and more
How to build an AI assistant with fenbs context in Python
- Create a free account and add a project for the work your assistant will help with.
- Write the current tasks, decisions, and rules into fenbs. Those become the context your assistant reads.
- Connect an assistant to fenbs over MCP from Integrations. The integration guide walks through the setup for supported clients.
- 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.
- 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.