show-api-payload.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/env bash
  2. #
  3. # Show what actually gets sent to the AI API
  4. # This demonstrates why caching saves money
  5. #
  6. set -euo pipefail
  7. AGENT="${1:-build}"
  8. SESSION_ID="${2:-demo}"
  9. echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  10. echo " What Gets Sent to AI API (Anthropic/Claude)"
  11. echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  12. echo ""
  13. echo "Every single request sends this structure:"
  14. echo ""
  15. cat << 'EOF'
  16. {
  17. "model": "claude-sonnet-4-5-20250929",
  18. "max_tokens": 8192,
  19. "system": [
  20. {
  21. "type": "text",
  22. "text": "<SYSTEM PROMPT 1 - BASE INSTRUCTIONS>",
  23. "cache_control": {"type": "ephemeral"} ← CACHED HERE
  24. },
  25. {
  26. "type": "text",
  27. "text": "<SYSTEM PROMPT 2 - ENVIRONMENT + CUSTOM>",
  28. "cache_control": {"type": "ephemeral"} ← CACHED HERE
  29. }
  30. ],
  31. "messages": [
  32. {"role": "user", "content": "Your first message"},
  33. {"role": "assistant", "content": "AI's response"},
  34. {"role": "user", "content": "Your second message"} ← Only this changes!
  35. ],
  36. "tools": [
  37. /* 14 tool definitions with schemas */
  38. ]
  39. }
  40. EOF
  41. echo ""
  42. echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  43. echo ""
  44. WORKSPACE_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
  45. PROMPT_DIR="$WORKSPACE_ROOT/packages/opencode/src/session/prompt"
  46. echo "SYSTEM PROMPT 1 - Base Instructions (~1,735 tokens)"
  47. echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  48. echo "File: packages/opencode/src/session/prompt/anthropic.txt"
  49. echo ""
  50. head -20 "$PROMPT_DIR/anthropic.txt"
  51. echo ""
  52. echo "... (truncated, see full file for complete instructions) ..."
  53. echo ""
  54. echo ""
  55. echo "SYSTEM PROMPT 2 - Environment + Custom Instructions"
  56. echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  57. echo ""
  58. echo "1. Environment Info (~40 tokens):"
  59. echo " - Working directory"
  60. echo " - Git repo status"
  61. echo " - Platform"
  62. echo " - Current date"
  63. echo ""
  64. echo "2. Project Tree (~500+ tokens):"
  65. if [[ -d "$WORKSPACE_ROOT/.git" ]]; then
  66. echo ""
  67. git ls-files 2>/dev/null | head -20 | sed 's/^/ - /'
  68. echo " ... (up to 200 files listed)"
  69. else
  70. echo " (No git repo)"
  71. fi
  72. echo ""
  73. echo "3. Custom Instructions (~273 tokens):"
  74. echo " - AGENTS.md"
  75. echo " - CLAUDE.md"
  76. echo " - Any files from config.instructions"
  77. echo ""
  78. echo ""
  79. echo "TOOL DEFINITIONS (~5,274 tokens)"
  80. echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  81. echo ""
  82. echo "Each tool includes:"
  83. echo " - Name and description"
  84. echo " - JSON Schema for parameters"
  85. echo " - Validation rules"
  86. echo ""
  87. echo "Example tool definition:"
  88. cat << 'EOF'
  89. {
  90. "name": "read",
  91. "description": "Read files from the filesystem. Can read multiple...",
  92. "input_schema": {
  93. "type": "object",
  94. "properties": {
  95. "paths": {
  96. "type": "array",
  97. "items": {"type": "string"},
  98. "description": "Array of file paths to read..."
  99. }
  100. },
  101. "required": ["paths"]
  102. }
  103. }
  104. EOF
  105. echo ""
  106. echo "× 14 tools = ~5,274 tokens"
  107. echo ""
  108. echo ""
  109. echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  110. echo " WHY YOU NEED ALL OF THIS"
  111. echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  112. echo ""
  113. echo "❌ WITHOUT system prompts:"
  114. echo " - AI doesn't know it's OpenCode"
  115. echo " - No instructions on how to use tools"
  116. echo " - No context about your project"
  117. echo " - Can't follow coding standards"
  118. echo ""
  119. echo "✅ WITH system prompts (cached):"
  120. echo " - AI knows its purpose and capabilities"
  121. echo " - Knows which tools are available"
  122. echo " - Understands your project structure"
  123. echo " - Follows your custom instructions"
  124. echo ""
  125. echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  126. echo " COST COMPARISON"
  127. echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  128. echo ""
  129. echo "WITHOUT CACHING (10 requests):"
  130. echo " Request 1: 8,000 tokens × $0.003/1K = $0.024"
  131. echo " Request 2: 8,000 tokens × $0.003/1K = $0.024"
  132. echo " Request 3: 8,000 tokens × $0.003/1K = $0.024"
  133. echo " ..."
  134. echo " Total: 80,000 tokens = $0.240"
  135. echo ""
  136. echo "WITH CACHING (10 requests):"
  137. echo " Request 1: 8,000 cache write × $0.00375/1K = $0.030"
  138. echo " Request 2: 8,000 cache read × $0.0003/1K = $0.0024"
  139. echo " Request 3: 8,000 cache read × $0.0003/1K = $0.0024"
  140. echo " ..."
  141. echo " Total: $0.030 + (9 × $0.0024) = $0.0516"
  142. echo ""
  143. echo " SAVINGS: $0.240 - $0.0516 = $0.1884 (78% cheaper!)"
  144. echo ""
  145. echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
  146. echo ""
  147. echo "The cache contains REQUIRED context that MUST be sent"
  148. echo "with every request. Without it, the AI can't function."
  149. echo ""
  150. echo "You're not paying extra - you're SAVING money! 💰"
  151. echo ""