Build an Abliterated AI Chatbot in Python, Step by Step
A chatbot is a loop around one API call: keep a list of messages, send it, print the reply, append it. This guide builds that loop with streaming and then prices a conversation in tokens.
Last updated:

Summary
- The whole bot is about 40 lines of Python using the standard OpenAI SDK.
- History must be resent every turn, so trim it before it gets expensive.
- A 15-turn chat with a short system prompt costs under one cent at $0.15 and $0.50 per 1M tokens.
- Persona and tone live in the system message; adult content is for lawful adult use only.
What you need
To build an abliterated AI chatbot you need Python 3.9 or newer, the openai package, and an API key. Install the package, then create a key on the key page. New users can claim $0.50 of trial credit with one click after sign-up: it lasts 7 days and needs no payment details.
pip install openai
export CHAT_KEY="paste-your-key"
The endpoint is https://api.abliteratedaichat.com/v1 and the model ID is uncensored. The model is an open-weight model run by an independent provider, tuned to answer without content refusals for lawful adult use. That makes it a fit for fiction, roleplay and companion-style apps for adults. It does not allow sexual content involving minors, and a bot you ship should state its age rules in your own terms.
Step 1: one reply
Start without history or streaming. Prove the key and the URL work first.
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.abliteratedaichat.com/v1",
api_key=os.environ["CHAT_KEY"],
)
r = client.chat.completions.create(
model="uncensored",
messages=[
{"role": "system", "content": "You are Mara, a dry-witted ship's navigator."},
{"role": "user", "content": "Where are we headed?"},
],
)
print(r.choices[0].message.content)
If this prints a reply in character, the integration works. If you get an authentication error, the key is wrong or the Bearer prefix is missing on a raw request. If you get 429 with rate_limited or concurrency, slow down: trial keys allow 2 concurrent requests and 60 per minute.
Step 2: memory and a loop
The API remembers nothing between calls. A chatbot is a growing list of messages that you send in full each time. The first item is the system message, which sets personality, rules and format.
SYSTEM = (
"You are Mara, a dry-witted ship's navigator in a sci-fi story. "
"Stay in character. Keep replies under 120 words."
)
history = [{"role": "system", "content": SYSTEM}]
while True:
user = input("you: ").strip()
if user in {"quit", "exit"}:
break
history.append({"role": "user", "content": user})
r = client.chat.completions.create(model="uncensored", messages=history)
text = r.choices[0].message.content
history.append({"role": "assistant", "content": text})
print("mara:", text)
Write the system prompt as instructions to a capable colleague: who the character is, what the setting is, how long to answer and which topics are out of scope for your app. Models without a refusal reflex do what the prompt says, so the prompt is your only safety rail. Put your own rules there.
Step 3: streaming
Waiting for a full reply feels slow. With stream=True tokens print as they arrive. The final chunk carries usage, so you can still track cost.
def chat_turn(history):
stream = client.chat.completions.create(
model="uncensored", messages=history, stream=True
)
parts, usage = [], None
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
piece = chunk.choices[0].delta.content
parts.append(piece)
print(piece, end="", flush=True)
if getattr(chunk, "usage", None):
usage = chunk.usage
print()
return "".join(parts), usage
Replace the call inside the loop with text, usage = chat_turn(history) and print usage.prompt_tokens and usage.completion_tokens when it is not None. One thing to remember: a streamed request keeps a concurrency slot until the stream ends. A full key has 8 slots; a web app with many users needs a queue or a limit per user.
Step 4: keep the history from growing without end
Each turn resends everything, so cost and latency climb. A simple policy keeps the system message and the last N messages.
def trimmed(history, keep=16):
system, rest = history[0], history[1:]
return [system] + rest[-keep:]
Call chat_turn(trimmed(history)). For a character with a long story, add a summary message that you refresh every 20 turns by asking the model to compress the older messages into five sentences. The window is 100,000 tokens, prompt plus completion, so you will rarely hit a hard limit, but cost and drift both argue for trimming well before that.
Writing the persona prompt
The system message does most of the work in a character bot, so spend time on it. A useful structure has four parts: who the character is, the setting and tone, formatting and length rules, and limits.
SYSTEM = """You are Mara, the navigator of the freighter Kestrel.
Setting: a sci-fi story for adult readers. Tone: dry, tired, loyal.
Format: speak in first person, at most 120 words, no stage directions in brackets.
Limits: stay in character; if the user asks something outside the story, answer briefly and return to it.
Never write sexual content involving minors."""
Test it with five awkward inputs: a one-word message, a message in another language, a request to break character, a very long paste and a message that contradicts earlier facts. Adjust the prompt after each failure. Change one sentence at a time so you can tell which edit helped.
What a conversation costs
Use realistic numbers. System prompt: 250 tokens. Each user message: 80 tokens. Each reply: 220 tokens. Fifteen turns.
Input on turn k is 250 + 80 + 300 x (k - 1) = 330 + 300 x (k - 1), because earlier turns add 300 tokens each. Summing for k = 1 to 15 gives 15 x 330 + 300 x 105 = 4,950 + 31,500 = 36,450 input tokens. Output is 15 x 220 = 3,300 tokens.
- Input: 36,450 x $0.15 / 1M = $0.0054675
- Output: 3,300 x $0.50 / 1M = $0.00165
- Total: about $0.0071 per conversation
A $20 top-up pays for roughly 2,800 conversations of that length. Failed or refused requests are not charged. If you trim history to the last 16 messages, input stops growing after the first 8 turns, which saves most of the cost in longer chats. The pricing page lists top-up tiers: $20 to $500, with a 5% bonus from $50, 10% from $100 and 15% from $200, paid in crypto (USDT on TRC20 or USDC on Base).
Where to go next
Serving it to other people
To serve the bot to others, keep the key on your server and expose your own endpoint to the browser. The browser sends the user message to your backend, your backend builds the message list and calls the API, and you forward the streamed tokens to the page. Never ship the API key in front-end code.
Plan for limits. A full key allows 8 concurrent requests and 300 per minute, and each stream holds a slot until it ends. For a hobby site that is plenty. For a busy one, queue requests, return a polite wait message on 429 and cap each user to one open stream. Store chats on your side if you want users to return to them later, and tell them what you keep.
Next features
Add JSON mode if you want structured side data such as mood or inventory in each reply, with response_format set to {"type":"json_object"}. Add tool calling if the bot should look something up or roll dice. Both are covered in the docs. For production patterns such as retries, key storage and moderation of your own users, the integration guide goes deeper.
Before launch, test the failure paths: an empty message, a very long message near the 8 MB body limit, a 429 with Retry-After: 5, and a network drop in the middle of a stream. A bot that survives those is ready for people.
More guides
Common inquiries
Does the API remember past messages?
No. You resend the conversation each request, usually as a list of messages with the system message first.
How much does one chat cost?
A 15-turn chat with a 250-token system prompt and short replies is about $0.0071 at $0.15 and $0.50 per 1M tokens.
Can I stream replies to a web page?
Yes. Set stream to true and forward the server-sent events from your backend. Mind the concurrency limit of 8 on a full key.
Can the chatbot produce content about minors?
No. Sexual content involving minors is never allowed. The service is for lawful adult use.
Access key in a single step
Register, copy the credential, update the endpoint URL. That is the entire process.