{"record":{"id":"e78e46b7df45e1c2","repo":"mastra-ai/mastra","slug":"countcoresystemmessagetokens-system-message-conte","errorCode":null,"errorMessage":"countCoreSystemMessageTokens: System message content must be a string","messagePattern":"countCoreSystemMessageTokens: System message content must be a string","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/src/processors/processors/token-limiter.ts","lineNumber":235,"sourceCode":"    const idsToRemove = messages.filter(m => !keepIds.has(m.id)).map(m => m.id);\n    if (idsToRemove.length > 0) {\n      messageList.removeByIds(idsToRemove);\n    }\n  }\n\n  /**\n   * Count tokens for a system message. Accepts both untagged and tagged system messages\n   * read from `messageList.getAllSystemMessages()`. Only string content is supported.\n   */\n  private async countCoreSystemMessageTokens(message: CoreMessageV4): Promise<number> {\n    if (message.role !== 'system') {\n      throw new Error(\n        `countCoreSystemMessageTokens can only be used with system messages, received role: ${message.role}`,\n      );\n    }\n\n    if (typeof message.content !== 'string') {\n      throw new Error('countCoreSystemMessageTokens: System message content must be a string');\n    }\n\n    const tokenString = message.role + message.content;\n\n    return this.countTokens(tokenString) + TokenLimiterProcessor.TOKENS_PER_MESSAGE;\n  }\n\n  /**\n   * Count tokens for an input message, including overhead for message structure\n   */\n  private async countInputMessageTokens(message: MastraDBMessage): Promise<number> {\n    let tokenString = message.role;\n    let overhead = 0;\n    // Media is estimated rather than tokenized, so it is accumulated separately.\n    let mediaTokens = 0;\n\n    // Handle content based on MastraMessageV2 structure\n    let toolResultCount = 0; // Track tool results that will become separate messages","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/processors/processors/token-limiter.ts#L217-L253","documentation":"countCoreSystemMessageTokens only supports string content for system messages. Tagged or array-part system messages (content as parts array) cannot be counted by the simple role+content concatenation, so the helper throws when typeof message.content !== 'string'.","triggerScenarios":"processInputStep encounters a system message from getAllSystemMessages() whose content is an array of content parts (multi-part system message) instead of a plain string.","commonSituations":"Building system messages with image/tool-part content arrays; constructing CoreMessage manually with content: [{type:'text',...}]; passing structured content from a custom memory provider.","solutions":["Provide system message content as a plain string: { role: 'system', content: 'instructions...' }.","Flatten multi-part system messages into a single string of concatenated text parts before adding them.","Upgrade @mastra/core in case a newer version counts array-content system messages.","Pre-tokenize the structured content yourself and adjust maxTokens accordingly."],"exampleFix":"// before\n{ role: 'system', content: [{ type: 'text', text: 'You are...' }] }\n// after\n{ role: 'system', content: 'You are...' }","handlingStrategy":"validation","validationCode":"function assertStringSystemContent(msg) {\n  if (msg.role === 'system' && typeof msg.content !== 'string') {\n    throw new Error('System message content must be a plain string');\n  }\n}","typeGuard":"function hasStringSystemContent(m) {\n  return m.role === 'system' && typeof m.content === 'string';\n}","tryCatchPattern":null,"preventionTips":["Always build system messages with string content.","Flatten content-part arrays into text before assigning to system messages.","Add a schema/lint check on message construction helpers."],"tags":["token-limit","processors","content-format","validation"],"backgroundTag":"invalid-argument-type","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}