0Pricing
AI Agents with LangChain & Autonomous Workflows · บทเรียน

การใช้โทเคนและการตรวจสอบค่าใช้จ่าย

วัดจำนวนโทเคนที่เอเจนต์ใช้และค่าใช้จ่ายแบบเรียลไทม์ ตั้งงบประมาณ และค้นหาขั้นตอนที่มีค่าใช้จ่ายสูงเพื่อเพิ่มประสิทธิภาพการใช้จ่ายในระบบจริง

การใช้โทเคนและการตรวจสอบค่าใช้จ่าย เป็นบทเรียน AI Agents with LangChain & Autonomous Workflows ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AI Agents with LangChain & Autonomous Workflows และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AI Agents with LangChain & Autonomous Workflows มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Track Tokens and Cost

Agents that loop, retry, or use long context can quietly become expensive. Without visibility you only discover the bill at month end.

Token and cost monitoring turns spend into a metric you can watch, alert on, and optimize.

Prompt vs Completion Tokens

Every call splits into:

  • Prompt tokens: everything you send in (system prompt, context, history)
  • Completion tokens: what the model generates

They are usually priced differently, so track them separately.

The Callback Approach

LangChain exposes usage via callbacks. get_openai_callback aggregates tokens and cost for everything inside its context block.

from langchain_community.callbacks import get_openai_callback

with get_openai_callback() as cb:
    result = agent.invoke({'input': 'Summarize the report'})
    print(cb.total_tokens, cb.total_cost)

Reading the Breakdown

The callback object also exposes the split, which is what you log per request.

print('prompt:', cb.prompt_tokens)
print('completion:', cb.completion_tokens)
print('cost USD:', cb.total_cost)

Usage Inside Responses

Many chat models also attach a usage_metadata field to the response, useful when you call the model directly without a callback.

resp = llm.invoke('Hello there')
print(resp.usage_metadata)

Estimating Before You Send

To stay under a budget, estimate tokens before calling the model using a tokenizer like tiktoken. This catches oversized prompts early.

import tiktoken
enc = tiktoken.encoding_for_model('gpt-4o-mini')
n = len(enc.encode(prompt_text))
print('approx tokens:', n)

Setting Budgets

Define a per-request and per-user token budget. If an estimate exceeds it, trim context, lower k in retrieval, or reject the request before paying for it.

MAX_TOKENS = 6000
if n > MAX_TOKENS:
    raise ValueError('Request exceeds token budget')

Per-Step Attribution

Agents make many sub-calls: tool selection, tool output processing, final answer. Wrapping each step's callback shows which step dominates cost, so you optimize the right one.

Logging to a Dashboard

Emit tokens and cost as structured logs or metrics (e.g. to Prometheus or LangSmith). Tag them with user, model, and route so you can slice spend.

log.info('llm_usage', extra={
    'tokens': cb.total_tokens,
    'cost': cb.total_cost,
    'route': 'support_agent'
})

Common Savings

Once you can see spend, the biggest wins are usually:

  • Smaller models for simple steps
  • Caching repeated calls
  • Trimming history and retrieved context
  • Stopping runaway agent loops with iteration limits

Alerting on Anomalies

Set alerts for sudden cost spikes — often a sign of a prompt-injection loop or a misbehaving tool. Catching it in minutes beats finding it on the invoice.

Quick Check

Test your cost monitoring knowledge.

Recap

You learned to observe agent spend:

  • Split prompt vs completion tokens
  • Use get_openai_callback and usage_metadata
  • Estimate with tiktoken and enforce budgets
  • Attribute cost per agent step
  • Log to dashboards and alert on spikes

Visibility into cost is the foundation for optimizing production agents.

คำถามที่พบบ่อย

บทเรียน “การใช้โทเคนและการตรวจสอบค่าใช้จ่าย” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การใช้โทเคนและการตรวจสอบค่าใช้จ่าย” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Agents with LangChain & Autonomous Workflows ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Agents with LangChain & Autonomous Workflows มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การใช้โทเคนและการตรวจสอบค่าใช้จ่าย”

วัดจำนวนโทเคนที่เอเจนต์ใช้และค่าใช้จ่ายแบบเรียลไทม์ ตั้งงบประมาณ และค้นหาขั้นตอนที่มีค่าใช้จ่ายสูงเพื่อเพิ่มประสิทธิภาพการใช้จ่ายในระบบจริง คุณปฏิบัติ AI Agents with LangChain & Autonomous Workflows ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Agents with LangChain & Autonomous Workflows หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Agents with LangChain & Autonomous Workflows บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การใช้โทเคนและการตรวจสอบค่าใช้จ่าย” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน AI Agents with LangChain & Autonomous Workflows นี้ได้ไหม

ได้ บทเรียน AI Agents with LangChain & Autonomous Workflows ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. LangSmith สำหรับการติดตามและตรวจสอบ
  2. การแก้ไขข้อบกพร่องในกระบวนการคิดของเอเจนต์
  3. การประเมินประสิทธิภาพของเอเจนต์
  4. การใช้โทเคนและการตรวจสอบค่าใช้จ่าย
← กลับไปที่ AI Agents with LangChain & Autonomous Workflows