We run AI agents in workplace chats. When a colleague @-mentions an agent, the message triggers a model, and the agent replies in the thread.
The agent wakes only on an explicit @-mention. Reading every message would fill its context and multiply token costs. Each time it wakes, it starts without context. The previous message might have arrived two minutes ago or eleven days ago. Giving the agent the right history, at the right price, is a harness problem. It sits in the code around the model; the model cannot solve it alone.
We treat this as a budget with three lines. The first is what sits in the prompt on every turn: tool schemas, instructions and recent messages. The second is each round trip to a tool, which sends the full context back to the model. The third is a wrong answer, which costs the most because a person acts on it. A good harness spends the first line on what the agent almost always needs, reserves the second for the long tail and curbs the third.
Our design has two layers:
@-mention
│
▼
┌────────────────────────────────────┐
│ 1. Recent messages in the prompt │──▶ answers about 3 mentions in 4
└────────────────────────────────────┘
│ the rest
▼
┌────────────────────────────────────┐
│ 2. One forgiving history tool │
│ · searches every chat │
│ · includes the agent's replies │
│ · shows photos as file paths │
└────────────────────────────────────┘
You are the agent
A colleague @-mentions you:
"What did I miss? What is the latest on the vendor review?"
Over the past eleven days, teammates posted messages about schedules and invoices. Buried in that thread, someone moved the review from Tuesday Aug 11 to Thursday Aug 13.
If you answer "Tuesday", your team acts on old information.
Here are four candidate designs:
Op = Literal["recent", "unread", "range", "search", "by_sender", "participants"]
# A. dispatch — One tool, six operations.
def chat_log(*, op: Op, n=20, text=None, sender=None, since=None, until=None) -> str: ...
# B. flat optional — Single signature, all parameters optional, no mode enum.
def chat_log(*, n=None, text=None, sender=None, since=None, until=None, unread=False): ...
# C. split — One tool per operation. Six separate schemas in the prompt.
def chat_recent(*, n=20): ...
def chat_search(*, text: str, n=50): ...
def chat_unread(*, n=100): ... # + by_sender, range, participants
# D. ambient context — Zero tools. Timestamped history injected directly in the prompt.
The schema is a fixed cost
We ran the four designs on the same synthetic questions, under identical prompts:
| Interface | Tool slots | Valid call rate | Avg turns | Accuracy | Prompt tokens |
|---|---|---|---|---|---|
| A dispatch | 1 | 52.2% | 3.00 | 90.0% | 2,076 |
| B flat optional | 2 | 78.3% | 2.43 | 100.0% | 1,405 |
| C split tools | 6 | 55.0% | 2.97 | 96.7% | 2,823 |
| D ambient context | 0 | — | 1.00 | 100.0% | 379 (p50) |
Blue is better, red is worse, within each column. Synthetic questions, identical prompts.
Every tool schema costs prompt tokens on every turn, even when the agent never calls the tool. C (split tools) follows standard API practice, with one function per operation; its six schemas cost 2,823 prompt tokens on every request. B (flat optional parameters) provides the same capability in half the tokens. It also scored 100%, because a model with no mandatory op parameter cannot pick the wrong mode.
D (ambient context) spends its tokens on the answer itself. It put timestamped history straight into the prompt and answered every question in one turn, with no tool calls, at a median of 379 tokens.
D and the tools cover different ground. D is cheap because it carries only recent messages, but it cannot reach older history or other chats. A tool can reach anything, yet each call costs a round trip. Neither works well alone. We pair them: D for what the agent almost always needs, and B for the rest. Synthetic questions are tidy, so we tested that pairing on real ones.
Layer one: put the conversation in the prompt
When someone @-mentions the agent, the gateway puts recent channel history into the prompt, with timestamps:
----- BEGIN EPHEMERAL LINE GROUP CONTEXT -----
# 16 unread since 2026-06-18 10:46 (8d 8h ago)
[2026-06-19 16:09] Teammate A: ...
[2026-06-20 10:01] Teammate B: ...
[2026-06-22 10:51] Teammate B: ...
----- END EPHEMERAL LINE GROUP CONTEXT -----
----- BEGIN CURRENT LINE REQUEST -----
[Teammate A] What did I miss? What is the latest on the vendor review?
----- END CURRENT LINE REQUEST -----
On routine turns, the model reads a plain timeline without a schema or unfamiliar syntax, avoiding an extra tool round trip. The cheapest tool call is the one the agent never needs to make.
Real use bears this out. In group chats, the agent answered 74% of mentions without its history tool.
The window works because of how people use the agent. An eleven-day catch-up is rare in our internal chat data. The median mention arrives nine minutes after the previous one, and more than half have no messages from anyone else in between. People talk to the agent in bursts, as they would to a colleague at the next desk.

Figure 1. Chatter between one @-mention and the next, in our own LINE groups.
The same data settled a design question. Showing the agent "everything since it was last mentioned" seems cheaper and more complete. We tested three versions in replay; the fixed window of recent messages beat all three. For most mentions, the span since the last mention is nearly empty. The recent window retains the ongoing conversation. At about 379 tokens a turn, it offers the best value in the prompt.
A window can easily pollute the prompt, so we keep it small and bounded. It is built only when someone @-mentions the agent. It holds a fixed number of the newest messages, about 379 tokens, however busy the chat is. The window never replaces the tool; everything older stays behind the tool. Its first line tells the agent how many messages are unread and since when, so the agent knows when to reach for the tool. That line is the handoff between the two layers.
Layer two: design the tool for the long tail
The history tool takes over where the window stops. It handles the remaining quarter of mentions, where the agent must look beyond the recent conversation. In our logs, most of those requests asked the agent to find details in a different chat:
- "Read the finance group, where we talked about my brokerage account. What do I need to do now?"
- "We planned a trip in a chat I can't find."
- "Look in LINE and see what the shopping list for Seoul is."
The tool searches every chat the agent may read unless the agent specifies one. In our data, a search scoped to the current chat came back empty three times in four. Searching every chat cut that to 17%.
Answers also sit in two places that a standard message log omits: the agent's earlier replies, and photos and screenshots.
We hill-climbed the harness on real questions
Design arguments are cheap. We settle them with a replay benchmark built from real questions in our chats. Each question carries a checklist of required facts and forbidden claims. The harness replays the question against the chat history as it stood at that moment, using the real model and tools.
We run the benchmark like a training run: one change per step. Every question runs three times, because model outputs vary from run to run. A separate model grades each answer fact by fact, and we regrade a sample to check that the grader agrees with itself. We read transcripts only from the tuning questions, keeping a held-out set that we never inspect. A change stays only if it pays for itself in facts right, tool calls or cost.

Figure 2. Replay hill-climb on real questions from our own chats. Each question is averaged over three runs.
1. Include the agent's own replies. The agent's earlier answers hold some of the most useful facts in a chat, from shared Drive links to compiled lists. With its replies in the history, the share of facts right rose from 63% to 78%, and the share of answers with a false claim fell from 15% to 6%. Tool calls fell by more than a quarter, and cost per answer dropped by a quarter. The replies also show where a conversation stands. In one finance thread, the agent noticed an open follow-up question and answered it.
2. Show photos as file paths. Many facts arrive as photos or screenshots. The history shows each image as a workspace path, such as inbox/line/…/2026-10-13-1250-teammate-1.jpg, which the agent opens with its existing read tool. This places no export tool in the prompt and requires no extra round trip. This step cost slightly more per answer, because the agent inspects more images, but the median time to an answer fell by almost a fifth. We kept it.
3. Search every chat with one flat call. The tool has no operation modes. It takes only optional parameters (chat, text, sender, since, until, n). This is design B from the table, extended across chats. Facts right reached 79%, tool calls fell to 57% of the baseline, and cost per answer returned to three-quarters of the baseline.
On held-out questions, facts right rose from 72% to 79%, and tool calls fell by half. Across the entire climb, the median time to an answer dropped from about a minute to about half a minute.
Some ideas did not earn a place:
- "Everything since the last mention" lost across all three versions we tested, for the reason shown in Figure 1.
- Status headers indicating result completeness added prompt tokens without improving accuracy, once search covered every chat.
- Hiding the agent's short progress replies ("Looking now…") saved few tokens. Its long answers carry about 70% of its text, and those are the ones worth keeping.
Build the handler for what models send
Models ask by meaning: by topic ("vendor review"), person ("Jordan"), relative date ("last Thursday") or chat ("the finance group"). In production, the agent named a chat in nearly nine calls out of ten and searched by text in about half. It never asked for a time range. In more than half of its calls, it asked for 200 messages, the upper limit.

Figure 3. How the agent fills in its history tool, in production.
Models also paraphrase schemas. In one replay, a model that wanted recent messages sent:
model sends: {"chatId": "line-ops-planning", "limit": 100}
│
▼
┌────────────────────────────────────────────┐
│ handler │
│ · ignores keys the tool does not have │
│ · maps aliases limit → n │
│ · clamps numbers n above 200 → 200 │
│ · no chat named → searches every chat │
└────────────────────────────────────────────┘
│
▼
search runs; the result names its scope
It invented chatId and wrote limit for n. A strict validator would reject the call, and the agent would lose a turn. Our handler treats the schema as a guide: it ignores unknown keys, maps common aliases and clamps numbers such as n to their permitted range. With a flat schema, there is no mode for the model to omit. In production, the model has sent no unknown keys.
One rule matters above the rest: an error or an empty result must never look like an empty history. A model that reads a bare "No messages found." will tell the user, with confidence, that nothing was ever said:
"I checked the full available chat history … the history contains only your two requests."
Every result therefore states what the tool searched and where:
# 0 messages match text "70%" in the … chats you can read (bot replies included).
With that line, the agent knows the search was thorough, and it can try another word.
What we would tell another team
- Treat the prompt as a budget. Pay on every turn only for what the agent almost always needs.
- Pair a small window of recent messages with one history tool. The window answers about three mentions in four at a small, fixed cost, but never replaces the tool. Its header tells the agent when to use the tool.
- Read real questions before designing the tool. Ours asked about other chats, the agent's earlier answers and photos.
- Give the agent its own replies, and provide photos as paths that it can open with tools it already has.
- Keep arguments flat and optional. Make the handler forgiving, and make every empty result report where it looked.
- Measure each choice on real questions and grade fact by fact. Synthetic benchmarks flatter: ours scored 100%, but our benchmark of real questions started at 63%.
If your team runs on chat and you want an agent that can find things in it, get in touch.
