Dayward AIWeChat Mini Program
From Frontend Engineer to Agent Engineer in 30 Days
Week 1 ยท D6๐Ÿ”’ LockedAbout 6 hours

Messages, Context Engineering and Compression, Session Storage/Recovery/Forking (dg M06/M08/M09/M10)

Understand how messages are organized and passed around inside an agent, how to compress context once the window fills up, and how a session is persisted, recovered, and forked.

Today's goals 0/3

Sign in to tick these off and save your progress.

Today's Goals

  1. Draw a message's complete path from user input to being written into history
  2. Implement a simple context-compression strategy that fires automatically as the window nears full
  3. Implement persisting a session, resuming it to continue the conversation, and forking a new session from a given step

Tools learned to correct themselves yesterday, but D5 left a bill open: every tool call pushes two messages into messages, and the tool-result one is often a thousand-token blob of JSON. The better your tools, the faster the history grows, until it bursts the context window. Today we face that head on.

Plain-Language Walkthrough

The life of a message: an agent's memory is this array

Some classrooms keep a relay diary: one notebook circulates, and when your turn comes you have to page back through it to know what to write next. The thicker it gets the longer the paging takes, and you have no alternative. An agent's memory is that notebook โ€” D1 established that the model is stateless and that a multi-turn conversation is entirely the messages array you maintain client-side. Today we fill in its internal structure. Every message carries a role; D1 covered three (system, user, assistant), and D2's tools added a fourth, tool, which holds a tool's return value.

So a message's path runs: user input arrives and appends a user; the array goes to the model; the model decides to call a tool and the returned assistant carries tool_calls, appended as-is; the tool runs and its result is wrapped into a tool and appended; the array is resent, and only now does the model emit prose for the user, which is another assistant to append. One round with a tool in it writes four messages into the history, not one.

messages.js
// The agent's "memory" is this array: the whole of it is resent to the model every round
const history = [
  { role: 'system', content: 'You are an order assistant.' },
  { role: 'user', content: 'When will the order I placed three days ago arrive?' },
]
 
// One tool call pushes two messages: the model's call request plus the tool's result
function appendToolRound(history, call, result) {
  history.push({ role: 'assistant', content: '', tool_calls: [call] })
  history.push({ role: 'tool', tool_call_id: call.id, content: JSON.stringify(result) })
}
 
// A rough token estimate: one character counts as one token, deliberately on the high side.
// Underestimating means the compression threshold never fires, and that is the one
// mistake you cannot make here
const estimateTokens = (msgs) =>
  msgs.reduce((sum, m) => sum + (m.content ?? '').length + 4, 0)

That estimateTokens deserves a note. D1 put an English word at roughly 1.3 tokens, so counting characters directly lands above the true figure โ€” and the direction is a deliberate choice. Underestimate and real usage hits 100% while your number has only just passed 70%, the threshold never fires, and you find out via a hard error thrown back by the model.

Run the arithmetic and D5's warning gets concrete. One order-table lookup returns two or three thousand tokens; assume one tool call per round with an 800-token result, and with the prose on both sides a round books about 1,000 tokens. Thirty rounds is 30,000, and all of it is resent in full on round 31. What bursts a history is never what the user typed, it is what tools hand back to the model โ€” precisely the part that grows faster the better you make the agent. So the question is not whether it fills up, it is what to do when it does.

Sign in and link WeChat to read

Sign in, then link your WeChat mini program account on the account page. Linked accounts share one free article per day. Unlock additional articles with rewarded videos in the mini program.

Mini program QR code

Scan in WeChat to open Dayward AI. Open the same course and chapter, choose the rewarded video, then refresh this page after completion.

Interview questions

  • When a long conversation outgrows the context window, how do you compress it โ€” when do you trigger, what do you drop, and what do you keep?้•ฟๅฏน่ฏ้‡ŒไธŠไธ‹ๆ–‡ๆ”พไธไธ‹ไบ†๏ผŒไฝ ไผšๆ€ŽไนˆๅŽ‹็ผฉ๏ผŸไป€ไนˆๆ—ถๅ€™่งฆๅ‘ใ€ๅŽ‹ๆމไป€ไนˆใ€ไฟ็•™ไป€ไนˆ๏ผŸ
    Common in ChinaCommon overseasIntermediate#context-engineering#compression#cost

    ๐Ÿ”’ Unlock this chapter to see the answer keys.

  • What problems do session persistence, restore, and forking each solve, and what goes wrong in each?ไผš่ฏ็š„ๆŒไน…ๅŒ–ใ€ๆขๅคๅ’Œๅˆ†ๅ‰ๅˆ†ๅˆซ่งฃๅ†ณไป€ไนˆ้—ฎ้ข˜๏ผŸๅฎž็Žฐๆ—ถๅ„ๆœ‰ไป€ไนˆๅ‘๏ผŸ
    Common in ChinaCommon overseasIntermediate#session-management#persistence#forking

    ๐Ÿ”’ Unlock this chapter to see the answer keys.

  • Where do you draw the line between short-term context and long-term memory, and how do you decide where a given fact belongs?็ŸญๆœŸไธŠไธ‹ๆ–‡ๅ’Œ้•ฟๆœŸ่ฎฐๅฟ†็š„่พน็•Œๆ€Žไนˆๅˆ’๏ผŸไธ€ๆกไฟกๆฏ่ฏฅๅพ€ๅ“ชๆ”พ๏ผŒไฝ ็š„ๅˆคๆ–ญไพๆฎๆ˜ฏไป€ไนˆ๏ผŸ
    Common in ChinaCommon overseasBasic#memory#context-engineering

    ๐Ÿ”’ Unlock this chapter to see the answer keys.

  • How do context engineering and RAG relate, and what breaks if you do RAG without context management?ไธŠไธ‹ๆ–‡ๅทฅ็จ‹ๅ’Œ RAG ๆฃ€็ดขๆ˜ฏไป€ไนˆๅ…ณ็ณป๏ผŸๅชๅš RAG ไธๅšไธŠไธ‹ๆ–‡็ฎก็†ไผšๅ‡บไป€ไนˆ้—ฎ้ข˜๏ผŸ
    Common in ChinaCommon overseasDeep dive#context-engineering#rag#retrieval

    ๐Ÿ”’ Unlock this chapter to see the answer keys.

Comments