<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Application on Tarragon</title><link>https://tarrragon.github.io/blog/tags/application/</link><description>Recent content in Application on Tarragon</description><generator>Hugo -- gohugo.io</generator><language>zh-TW</language><copyright>Tarragon (CC BY 4.0)</copyright><lastBuildDate>Tue, 12 May 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://tarrragon.github.io/blog/tags/application/index.xml" rel="self" type="application/rss+xml"/><item><title>System Prompt</title><link>https://tarrragon.github.io/blog/llm/knowledge-cards/system-prompt/</link><pubDate>Tue, 12 May 2026 00:00:00 +0000</pubDate><guid>https://tarrragon.github.io/blog/llm/knowledge-cards/system-prompt/</guid><description>&lt;p>System prompt 的核心概念是「LLM application 中、由開發者預設、放在每次 conversation 最前面、不直接顯示給使用者的指令層」。常見用途包括設定模型角色（如「你是 senior Python engineer」）、規範輸出格式（如「always return JSON」）、加入 safety guideline。Chat-based LLM API（OpenAI、Anthropic 等）通常有專門的 &lt;code>role: &amp;quot;system&amp;quot;&lt;/code> message type。&lt;/p>
&lt;h2 id="概念位置">概念位置&lt;/h2>
&lt;p>LLM API call 的訊息結構：&lt;/p>





&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">messages = [
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl"> {role: &amp;#34;system&amp;#34;, content: &amp;#34;你是專業 code reviewer...&amp;#34;}, ← system prompt
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl"> {role: &amp;#34;user&amp;#34;, content: &amp;#34;請 review 這段 code: ...&amp;#34;},
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl"> {role: &amp;#34;assistant&amp;#34;, content: &amp;#34;...&amp;#34;}, ← 模型回答
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl"> {role: &amp;#34;user&amp;#34;, content: &amp;#34;...&amp;#34;}, ← 後續對話
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">6&lt;/span>&lt;span class="cl"> ...
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">7&lt;/span>&lt;span class="cl">]&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>System prompt 在 application 設計中的角色：&lt;/p>
&lt;table>
 &lt;thead>
 &lt;tr>
 &lt;th>用途&lt;/th>
 &lt;th>例子&lt;/th>
 &lt;/tr>
 &lt;/thead>
 &lt;tbody>
 &lt;tr>
 &lt;td>角色定義&lt;/td>
 &lt;td>&amp;ldquo;你是 senior Python engineer、專長 async / typing&amp;rdquo;&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>輸出格式約束&lt;/td>
 &lt;td>&amp;ldquo;always return JSON with keys: title, body, tags&amp;rdquo;&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>行為規範&lt;/td>
 &lt;td>&amp;ldquo;若不確定、明確說『我不知道』、不要編造&amp;rdquo;&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>工具使用指引&lt;/td>
 &lt;td>&amp;ldquo;When user asks about weather, call get_weather tool&amp;rdquo;&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>安全約束&lt;/td>
 &lt;td>&amp;ldquo;Do not generate executable shell commands&amp;rdquo;&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>上下文注入&lt;/td>
 &lt;td>&amp;ldquo;Current date: 2026-05-12; User language: zh-TW&amp;rdquo;&lt;/td>
 &lt;/tr>
 &lt;/tbody>
&lt;/table>
&lt;blockquote>
&lt;p>&lt;strong>事實查核註&lt;/strong>：不同 LLM vendor 對 system prompt 的處理機制不同（如部分模型把 system 跟 user 視為相同優先級、部分模型有特殊訓練讓 system 較高優先）、具體行為以該模型的&lt;a href="https://platform.openai.com/docs/api-reference/chat">官方文件&lt;/a>為準。&lt;/p>&lt;/blockquote>
&lt;h2 id="設計責任">設計責任&lt;/h2>
&lt;p>理解 system prompt 後可以解釋兩個現象：為什麼同一個模型在不同 LLM 應用中的「個性」差很多（system prompt 不同）、為什麼 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/prompt-injection/" data-link-title="Prompt Injection" data-link-desc="把惡意指令藏進 LLM 會讀到的內容、誘導 LLM 跑出非開發者預期行為的攻擊類別、OWASP LLM01 列入頭號威脅">prompt injection&lt;/a> 的主要目標是繞過 system prompt 的約束（攻擊者想讓模型不照原本指令走）。&lt;/p>
&lt;p>實務上、設計 LLM application 時、system prompt 是行為約束的第一層、但不是唯一防線（容易被 injection 繞過）；critical 行為應該在 application 層（如 tool call 的權限白名單、輸出驗證）加第二層防護。詳見 &lt;a href="https://tarrragon.github.io/blog/llm/06-security/prompt-injection-in-ide/" data-link-title="6.3 IDE 場景的 prompt injection" data-link-desc="個人 dev 場景下 IDE 寫 code 工作流的 prompt injection：codebase 內容、外部文件、剪貼簿作為攻擊面、跟雲端 LLM 場景的差異">6.3 IDE 場景的 prompt injection&lt;/a>。&lt;/p></description><content:encoded><![CDATA[<p>System prompt 的核心概念是「LLM application 中、由開發者預設、放在每次 conversation 最前面、不直接顯示給使用者的指令層」。常見用途包括設定模型角色（如「你是 senior Python engineer」）、規範輸出格式（如「always return JSON」）、加入 safety guideline。Chat-based LLM API（OpenAI、Anthropic 等）通常有專門的 <code>role: &quot;system&quot;</code> message type。</p>
<h2 id="概念位置">概念位置</h2>
<p>LLM API call 的訊息結構：</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">messages = [
</span></span><span class="line"><span class="ln">2</span><span class="cl">  {role: &#34;system&#34;, content: &#34;你是專業 code reviewer...&#34;},  ← system prompt
</span></span><span class="line"><span class="ln">3</span><span class="cl">  {role: &#34;user&#34;,   content: &#34;請 review 這段 code: ...&#34;},
</span></span><span class="line"><span class="ln">4</span><span class="cl">  {role: &#34;assistant&#34;, content: &#34;...&#34;},  ← 模型回答
</span></span><span class="line"><span class="ln">5</span><span class="cl">  {role: &#34;user&#34;,   content: &#34;...&#34;},     ← 後續對話
</span></span><span class="line"><span class="ln">6</span><span class="cl">  ...
</span></span><span class="line"><span class="ln">7</span><span class="cl">]</span></span></code></pre></div><p>System prompt 在 application 設計中的角色：</p>
<table>
  <thead>
      <tr>
          <th>用途</th>
          <th>例子</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>角色定義</td>
          <td>&ldquo;你是 senior Python engineer、專長 async / typing&rdquo;</td>
      </tr>
      <tr>
          <td>輸出格式約束</td>
          <td>&ldquo;always return JSON with keys: title, body, tags&rdquo;</td>
      </tr>
      <tr>
          <td>行為規範</td>
          <td>&ldquo;若不確定、明確說『我不知道』、不要編造&rdquo;</td>
      </tr>
      <tr>
          <td>工具使用指引</td>
          <td>&ldquo;When user asks about weather, call get_weather tool&rdquo;</td>
      </tr>
      <tr>
          <td>安全約束</td>
          <td>&ldquo;Do not generate executable shell commands&rdquo;</td>
      </tr>
      <tr>
          <td>上下文注入</td>
          <td>&ldquo;Current date: 2026-05-12; User language: zh-TW&rdquo;</td>
      </tr>
  </tbody>
</table>
<blockquote>
<p><strong>事實查核註</strong>：不同 LLM vendor 對 system prompt 的處理機制不同（如部分模型把 system 跟 user 視為相同優先級、部分模型有特殊訓練讓 system 較高優先）、具體行為以該模型的<a href="https://platform.openai.com/docs/api-reference/chat">官方文件</a>為準。</p></blockquote>
<h2 id="設計責任">設計責任</h2>
<p>理解 system prompt 後可以解釋兩個現象：為什麼同一個模型在不同 LLM 應用中的「個性」差很多（system prompt 不同）、為什麼 <a href="/blog/llm/knowledge-cards/prompt-injection/" data-link-title="Prompt Injection" data-link-desc="把惡意指令藏進 LLM 會讀到的內容、誘導 LLM 跑出非開發者預期行為的攻擊類別、OWASP LLM01 列入頭號威脅">prompt injection</a> 的主要目標是繞過 system prompt 的約束（攻擊者想讓模型不照原本指令走）。</p>
<p>實務上、設計 LLM application 時、system prompt 是行為約束的第一層、但不是唯一防線（容易被 injection 繞過）；critical 行為應該在 application 層（如 tool call 的權限白名單、輸出驗證）加第二層防護。詳見 <a href="/blog/llm/06-security/prompt-injection-in-ide/" data-link-title="6.3 IDE 場景的 prompt injection" data-link-desc="個人 dev 場景下 IDE 寫 code 工作流的 prompt injection：codebase 內容、外部文件、剪貼簿作為攻擊面、跟雲端 LLM 場景的差異">6.3 IDE 場景的 prompt injection</a>。</p>
]]></content:encoded></item><item><title>Tool Use</title><link>https://tarrragon.github.io/blog/llm/knowledge-cards/tool-use/</link><pubDate>Tue, 12 May 2026 00:00:00 +0000</pubDate><guid>https://tarrragon.github.io/blog/llm/knowledge-cards/tool-use/</guid><description>&lt;p>Tool use 的核心概念是「LLM 不只生成文字、還能透過結構化呼叫外部工具來執行讀檔、查資料庫、發 API request、跑程式等動作」。它擴展 LLM 從「對話模型」變成「能影響真實世界的 agent」。實作上常見透過 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/function-calling/" data-link-title="Function Calling" data-link-desc="模型訓練階段建立的「呼叫工具」能力：知道何時該呼叫、傳什麼參數">function calling&lt;/a> 或 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/mcp/" data-link-title="MCP（Model Context Protocol）" data-link-desc="LLM application ↔ 外部 tool server 之間的標準化協議、複用 OpenAI 相容 API 的成功模式">MCP&lt;/a> 協定。&lt;/p>
&lt;h2 id="概念位置">概念位置&lt;/h2>
&lt;p>Tool use 的典型流程：&lt;/p>





&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="ln">1&lt;/span>&lt;span class="cl">1. 開發者定義 tools（每個 tool 含 name、description、parameters schema）
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">2&lt;/span>&lt;span class="cl">2. LLM 收到 user message 跟 tools 清單
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">3&lt;/span>&lt;span class="cl">3. LLM 決定要呼叫哪個 tool、生成結構化 tool call（JSON）
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">4&lt;/span>&lt;span class="cl">4. LLM client（不是模型本身）執行 tool call、得到結果
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="ln">5&lt;/span>&lt;span class="cl">5. tool 結果回灌進 conversation、模型基於結果繼續生成或再呼叫&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>關鍵特性：&lt;/p>
&lt;ol>
&lt;li>&lt;strong>模型本身不執行 tool&lt;/strong>：模型只生成 tool call JSON、實際執行由 client 或 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/mcp/" data-link-title="MCP（Model Context Protocol）" data-link-desc="LLM application ↔ 外部 tool server 之間的標準化協議、複用 OpenAI 相容 API 的成功模式">MCP server&lt;/a> 完成。&lt;/li>
&lt;li>&lt;strong>權限由 OS / user / sandbox 決定&lt;/strong>：模型再「同意」執行 &lt;code>rm -rf /&lt;/code>、實際能不能跑取決於跑 tool 的 process 權限。&lt;/li>
&lt;li>&lt;strong>副作用範圍跟 tool 設計強相關&lt;/strong>：tool 寫得越通用（如 &lt;code>run_shell&lt;/code>）、攻擊面越大；tool 寫得越窄（如 &lt;code>read_workspace_file&lt;/code>）、攻擊面越小。&lt;/li>
&lt;/ol>
&lt;p>Tool use 跟 function calling、MCP 的關係：&lt;/p>
&lt;table>
 &lt;thead>
 &lt;tr>
 &lt;th>層次&lt;/th>
 &lt;th>角色&lt;/th>
 &lt;/tr>
 &lt;/thead>
 &lt;tbody>
 &lt;tr>
 &lt;td>Tool use（概念）&lt;/td>
 &lt;td>廣義概念、LLM 能呼叫工具&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>Function calling&lt;/td>
 &lt;td>OpenAI 提出的 API 規範、用 JSON schema 定義 function&lt;/td>
 &lt;/tr>
 &lt;tr>
 &lt;td>&lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/mcp/" data-link-title="MCP（Model Context Protocol）" data-link-desc="LLM application ↔ 外部 tool server 之間的標準化協議、複用 OpenAI 相容 API 的成功模式">MCP&lt;/a>&lt;/td>
 &lt;td>Anthropic 推動的開放協議、定義 LLM client 跟 tool server 之間的通訊格式&lt;/td>
 &lt;/tr>
 &lt;/tbody>
&lt;/table>
&lt;h2 id="設計責任">設計責任&lt;/h2>
&lt;p>理解 tool use 後可以解釋三個現象：為什麼 LLM 「能跑 shell」其實是 client 跑、不是模型跑（職責切分）、為什麼 tool spec 設計直接影響攻擊面（spec 越鬆、injection 後果越大）、為什麼 &lt;a href="https://tarrragon.github.io/blog/llm/knowledge-cards/agent-loop/" data-link-title="Agent Loop" data-link-desc="LLM agent 自我循環的工作流：LLM 規劃下一步、執行 tool、看結果、再規劃下一步、直到任務完成或停止條件觸發">agent loop&lt;/a> 比單次 tool call 危險（多步 tool use 中 injection 累積）。&lt;/p>
&lt;p>設計 tool 跟 MCP server 時、權限白名單 + 副作用可逆性 + confirm 機制是基本配置；production 場景見 &lt;a href="https://tarrragon.github.io/blog/backend/07-security-data-protection/llm-prompt-injection-in-agent/" data-link-title="LLM Agent Prompt Injection 後果治理" data-link-desc="production LLM agent 場景的 prompt injection 後果：tool spec 設計、agent loop 限制、review checkpoint、跟 incident workflow 的接合">LLM Agent Prompt Injection 後果治理&lt;/a> 跟 &lt;a href="https://tarrragon.github.io/blog/llm/06-security/tool-use-permission-model/" data-link-title="6.2 tool use 與 MCP server 的權限模型" data-link-desc="個人 dev 場景下 tool use / MCP server 的副作用權限：檔案系統 / shell / 網路存取邊界、第三方 MCP 信任、副作用的可逆性">6.2 tool use 與 MCP server 的權限模型&lt;/a>。&lt;/p></description><content:encoded><![CDATA[<p>Tool use 的核心概念是「LLM 不只生成文字、還能透過結構化呼叫外部工具來執行讀檔、查資料庫、發 API request、跑程式等動作」。它擴展 LLM 從「對話模型」變成「能影響真實世界的 agent」。實作上常見透過 <a href="/blog/llm/knowledge-cards/function-calling/" data-link-title="Function Calling" data-link-desc="模型訓練階段建立的「呼叫工具」能力：知道何時該呼叫、傳什麼參數">function calling</a> 或 <a href="/blog/llm/knowledge-cards/mcp/" data-link-title="MCP（Model Context Protocol）" data-link-desc="LLM application ↔ 外部 tool server 之間的標準化協議、複用 OpenAI 相容 API 的成功模式">MCP</a> 協定。</p>
<h2 id="概念位置">概念位置</h2>
<p>Tool use 的典型流程：</p>





<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="ln">1</span><span class="cl">1. 開發者定義 tools（每個 tool 含 name、description、parameters schema）
</span></span><span class="line"><span class="ln">2</span><span class="cl">2. LLM 收到 user message 跟 tools 清單
</span></span><span class="line"><span class="ln">3</span><span class="cl">3. LLM 決定要呼叫哪個 tool、生成結構化 tool call（JSON）
</span></span><span class="line"><span class="ln">4</span><span class="cl">4. LLM client（不是模型本身）執行 tool call、得到結果
</span></span><span class="line"><span class="ln">5</span><span class="cl">5. tool 結果回灌進 conversation、模型基於結果繼續生成或再呼叫</span></span></code></pre></div><p>關鍵特性：</p>
<ol>
<li><strong>模型本身不執行 tool</strong>：模型只生成 tool call JSON、實際執行由 client 或 <a href="/blog/llm/knowledge-cards/mcp/" data-link-title="MCP（Model Context Protocol）" data-link-desc="LLM application ↔ 外部 tool server 之間的標準化協議、複用 OpenAI 相容 API 的成功模式">MCP server</a> 完成。</li>
<li><strong>權限由 OS / user / sandbox 決定</strong>：模型再「同意」執行 <code>rm -rf /</code>、實際能不能跑取決於跑 tool 的 process 權限。</li>
<li><strong>副作用範圍跟 tool 設計強相關</strong>：tool 寫得越通用（如 <code>run_shell</code>）、攻擊面越大；tool 寫得越窄（如 <code>read_workspace_file</code>）、攻擊面越小。</li>
</ol>
<p>Tool use 跟 function calling、MCP 的關係：</p>
<table>
  <thead>
      <tr>
          <th>層次</th>
          <th>角色</th>
      </tr>
  </thead>
  <tbody>
      <tr>
          <td>Tool use（概念）</td>
          <td>廣義概念、LLM 能呼叫工具</td>
      </tr>
      <tr>
          <td>Function calling</td>
          <td>OpenAI 提出的 API 規範、用 JSON schema 定義 function</td>
      </tr>
      <tr>
          <td><a href="/blog/llm/knowledge-cards/mcp/" data-link-title="MCP（Model Context Protocol）" data-link-desc="LLM application ↔ 外部 tool server 之間的標準化協議、複用 OpenAI 相容 API 的成功模式">MCP</a></td>
          <td>Anthropic 推動的開放協議、定義 LLM client 跟 tool server 之間的通訊格式</td>
      </tr>
  </tbody>
</table>
<h2 id="設計責任">設計責任</h2>
<p>理解 tool use 後可以解釋三個現象：為什麼 LLM 「能跑 shell」其實是 client 跑、不是模型跑（職責切分）、為什麼 tool spec 設計直接影響攻擊面（spec 越鬆、injection 後果越大）、為什麼 <a href="/blog/llm/knowledge-cards/agent-loop/" data-link-title="Agent Loop" data-link-desc="LLM agent 自我循環的工作流：LLM 規劃下一步、執行 tool、看結果、再規劃下一步、直到任務完成或停止條件觸發">agent loop</a> 比單次 tool call 危險（多步 tool use 中 injection 累積）。</p>
<p>設計 tool 跟 MCP server 時、權限白名單 + 副作用可逆性 + confirm 機制是基本配置；production 場景見 <a href="/blog/backend/07-security-data-protection/llm-prompt-injection-in-agent/" data-link-title="LLM Agent Prompt Injection 後果治理" data-link-desc="production LLM agent 場景的 prompt injection 後果：tool spec 設計、agent loop 限制、review checkpoint、跟 incident workflow 的接合">LLM Agent Prompt Injection 後果治理</a> 跟 <a href="/blog/llm/06-security/tool-use-permission-model/" data-link-title="6.2 tool use 與 MCP server 的權限模型" data-link-desc="個人 dev 場景下 tool use / MCP server 的副作用權限：檔案系統 / shell / 網路存取邊界、第三方 MCP 信任、副作用的可逆性">6.2 tool use 與 MCP server 的權限模型</a>。</p>
]]></content:encoded></item></channel></rss>