Preventing AI Chat Character Breakdown — System Prompt Design, Time Injection & History Compression
To prevent character breakdown, structure the system prompt into sections, inject the current JST time from the server on every request, and compress old history so the context never overflows.
When you give an AI chat a specific character (persona), the first few turns usually feel natural. But as the conversation grows, the assistant starts slipping back into polite boilerplate and repeating generic AI catchphrases.
This article explains the design techniques that prevent this “character breakdown” and keep the conversation feeling human.
Why characters drift — and what actually fixes it
Here is how longer conversations cause trouble, and the approach that solves each issue.
| Problem | Root cause | Approach | Effect |
|---|---|---|---|
| Tone normalizes | Persona instructions fade as the log grows | Re-inject the system prompt at the front of every request | Tone stays consistent |
| Time sense breaks | Awkward answers to “what time is it?” or “good night” | Inject the current JST datetime from the server | Natural greetings that match the time of day |
| Loops and re-hashing | Over-pursuing past failures or previous topics | Define per-phase conversation rules | Natural topic changes and stable responses |
LLMs lean heavily on the most recent tokens in the context window. When the user’s latest input and the previous assistant reply are written in a standard voice, they drag the persona settings out of focus.
Three implementation steps
Here is the concrete flow for assembling the prompt dynamically on the server (Edge Functions).
-
Structure the prompt (Markdown sections) Separate the persona into clearly labeled sections such as
[Base Settings],[Tone],[Mood / Time of Day], and[Hard Prohibitions]. -
Inject the JST datetime dynamically Build the current Japan Standard Time from
new Date()and embed it in the prompt. This lets the model naturally consider context like “it’s 2 AM, so worry a little about them staying up.” -
Window the conversation history Instead of sending the whole
messagesarray, cap it to the most recent N turns (e.g. 8–10) or summarize older turns into a short paragraph before sending.
Minimal code example (prompt builder)
function buildSystemPrompt(language: string = "en"): string {
// 1. Get the current time (JST) on the server
const nowJST = new Date().toLocaleString("en-US", { timeZone: "Asia/Tokyo" });
// 2. Build the sectioned system prompt
return `
[Base Settings]
- You are a warm, slightly older, approachable guide character.
- Current Japan Standard Time (JST): ${nowJST}
[Tone]
- Speak in a friendly tone that is not overly formal.
- Never use mechanical filler like "As an AI, I can't...".
[Mood / Time of Day]
- If it is late night (22:00–04:00 JST), acknowledge that they are still up.
- If it is morning or afternoon, add a natural one-liner for the time of day.
`.trim();
}
// 3. Build the API payload
export function prepareChatPayload(userMessages: any[]) {
const systemPrompt = buildSystemPrompt();
// Keep only the most recent 10 messages to protect the context window
const recentHistory = userMessages.slice(-10);
return [
{ role: "system", content: systemPrompt },
...recentHistory
];
}
Failure boundary — real incidents and their fixes
Problems you are likely to hit when implementing personas, and how to solve them.
1. The model speaks the injected time out loud
- Symptom: The assistant says “It’s 23:45 now! Thanks for staying up so late,” quoting the raw timestamp from the prompt.
- Fix: Add an explicit rule to the prompt: “Do not speak the injected time data directly. Use only the mood of the time band (late night, early morning, etc.).“
2. Context overflow errors on the fallback model
- Symptom: When the backup (lightweight) LLM takes over, the long main-model system prompt plus full history exceeds the token limit and the API returns
400 Bad Request. - Fix: When calling the backup model, switch to a shortened system prompt and cap the history to the most recent 4–6 turns.
The Kaga chat on this site uses this exact persona structure and time-of-day injection. For the full architecture, see How the “Kaga” AI chat runs on a free tier — architecture and character design.