DEV Community

Cover image for วิธีใช้ Mistral Large 4 API?
Thanawat Wongchai
Thanawat Wongchai

Posted on Originally published at apidog.com

วิธีใช้ Mistral Large 4 API?

Mistral Large 4 เปิดตัวบน Mistral API เมื่อวันที่ 6 ตุลาคม 2026 ก่อนโมเดลแบบ Open Weights ราวสามสัปดาห์ หากต้องการทดลอง “Le Chonk” ที่มีพารามิเตอร์ 1 ล้านล้านตัวตอนนี้ API คือวิธีเดียว และยังมีต้นทุนต่ำที่สุด: Mistral ระบุราคาพรีวิวสาธารณะที่ $0.68 ต่อ 1 ล้านโทเค็นอินพุต และ $2.09 ต่อ 1 ล้านโทเค็นเอาต์พุต หรือครึ่งหนึ่งของราคาปกติ $1.36 / $4.18

ลองใช้ Apidog วันนี้

คู่มือนี้จะพาคุณตั้งแต่การเรียก API ครั้งแรก ไปจนถึง reasoning chunks, อินพุตรูปภาพ, function calling, JSON output และการคำนวณต้นทุน คุณสามารถบันทึกและเล่นซ้ำทุกคำขอใน Apidog เพื่อเปรียบเทียบ Large 4 กับโมเดลที่ใช้อยู่ได้อย่างเป็นระบบ

เพิ่งรู้จักโมเดลนี้ใช่ไหม? อ่าน Mistral กลับมาแล้ว: Le Chonk เอาชนะ GPT-6 Astra และ Claude ในการโจมตีทางไซเบอร์ เพื่อดูการเปรียบเทียบและบริบทเบื้องหลังหัวข้อข่าวไซเบอร์

สิ่งที่คุณต้องมี

รายการ ค่า
Base URL https://api.mistral.ai/v1
การยืนยันตัวตน Authorization: Bearer $MISTRAL_API_KEY
Model ID mistral-large-4 (ชื่อเรียก mistral-large-4-0)
Main endpoint POST /v1/chat/completions
Context window 1 ล้านโทเค็น
ประเภทอินพุต ข้อความ, รูปภาพ
Python SDK pip install mistralai
TypeScript SDK npm install @mistralai/mistralai

ขั้นตอนที่ 1: รับ API key

  1. ลงชื่อเข้าใช้ Mistral Studio ซึ่งเดิมชื่อ La Plateforme
  2. เปิด API Keys แล้วสร้างคีย์ใหม่
  3. ตั้งชื่อคีย์ตามการใช้งาน เช่น local-dev หรือ ci-staging
  4. คัดลอกคีย์ทันที เพราะ Studio จะแสดงให้เห็นเพียงครั้งเดียว
  5. ส่งออกคีย์ในเชลล์:
export MISTRAL_API_KEY="your-key-here"
Enter fullscreen mode Exit fullscreen mode

อย่า commit key ลง source control หากต้องใช้คีย์กับหลายสภาพแวดล้อม ให้อ่าน แนวทางปฏิบัติที่ดีที่สุดในการจัดการ API key สำหรับแนวทางเรื่องการหมุนเวียนและการกำหนดขอบเขตสิทธิ์

ขั้นตอนที่ 2: เรียกใช้งานครั้งแรก

เริ่มจาก curl เพื่อแยกปัญหาเรื่อง SDK และตรวจสอบว่า key ใช้งานได้:

curl https://api.mistral.ai/v1/chat/completions \
  -H "Authorization: Bearer $MISTRAL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "mistral-large-4",
    "messages": [
      {
        "role": "user",
        "content": "Give me three edge cases to test on a pagination API."
      }
    ]
  }'
Enter fullscreen mode Exit fullscreen mode

การตอบกลับที่สำเร็จจะมี:

  • choices[0].message.content สำหรับคำตอบ
  • usage.prompt_tokens
  • usage.completion_tokens
  • usage.total_tokens

หากพบข้อผิดพลาด:

  • 401: key ไม่ถูกต้อง, ไม่มี key หรือไม่ได้ export ตัวแปรแวดล้อม
  • 404: ตรวจสอบว่าใช้ Model ID เป็น mistral-large-4 ถูกต้อง

เรียกใช้งานเดียวกันใน Python

ติดตั้ง SDK ก่อน:

pip install mistralai
Enter fullscreen mode Exit fullscreen mode

จากนั้นเรียก API:

import os
from mistralai import Mistral

client = Mistral(api_key=os.environ["MISTRAL_API_KEY"])

response = client.chat.complete(
    model="mistral-large-4",
    messages=[
        {
            "role": "user",
            "content": "Give me three edge cases to test on a pagination API.",
        }
    ],
)

print(response.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

เรียกใช้งานใน TypeScript

ติดตั้งแพ็กเกจ:

npm install @mistralai/mistralai
Enter fullscreen mode Exit fullscreen mode

จากนั้นใช้โค้ดต่อไปนี้:

import { Mistral } from "@mistralai/mistralai";

const client = new Mistral({
  apiKey: process.env.MISTRAL_API_KEY,
});

const response = await client.chat.complete({
  model: "mistral-large-4",
  messages: [
    {
      role: "user",
      content: "Give me three edge cases to test on a pagination API.",
    },
  ],
});

console.log(response.choices[0].message.content);
Enter fullscreen mode Exit fullscreen mode

ขั้นตอนที่ 3: บันทึกคำขอใน Apidog

เมื่อเริ่มเปรียบเทียบหลายโมเดล การพิมพ์ curl ซ้ำจะจัดการยากขึ้น ใช้ Apidog เพื่อบันทึกคำขอและรัน regression test ได้:

  1. สร้าง HTTP request ใหม่:
   POST https://api.mistral.ai/v1/chat/completions
Enter fullscreen mode Exit fullscreen mode
  1. สร้าง environment variable ชื่อ MISTRAL_API_KEY
  2. เพิ่ม header:
   Authorization: Bearer {{MISTRAL_API_KEY}}
Enter fullscreen mode Exit fullscreen mode
  1. วาง JSON body จากขั้นตอนก่อนหน้า แล้วกด Send
  2. ทำสำเนาคำขอ เปลี่ยนค่า model เป็นโมเดลที่ใช้อยู่ เช่น mistral-medium-3-5
  3. รันคำขอทั้งสองด้วย prompt เดียวกัน

ตอนนี้คุณมี baseline สำหรับเปรียบเทียบ:

  • เนื้อหาคำตอบ
  • HTTP status
  • เวลาในการตอบกลับ
  • ขนาด response
  • ค่า usage และจำนวนโทเค็น

เพิ่ม assertion หลังการตอบกลับเพื่อตรวจสอบว่า choices[0].message.content ไม่ว่างเปล่า แล้วใช้คำขอนี้เป็น smoke test ทุกครั้งที่ Mistral อัปเดตโมเดลพรีวิว

ขั้นตอนที่ 4: เปิดและปิดการให้เหตุผล

Large 4 เป็นโมเดลไฮบริดที่รองรับทั้งคำตอบเร็วและการให้เหตุผลแบบหลายขั้นตอน ควบคุมด้วย reasoning_effort:

ค่า พฤติกรรม เหมาะสำหรับ
"none" ใช้การคิดน้อยที่สุด และไม่มี thinking chunk ใน response แชท, สกัดข้อมูล, จำแนกประเภท, งานที่ไวต่อความหน่วง
"high" ส่ง thinking chunk ก่อนคำตอบสุดท้าย ดีบัก, วางแผนหลายขั้นตอน, คณิตศาสตร์, รีวิวโค้ด

ตัวอย่างเปิด reasoning:

curl https://api.mistral.ai/v1/chat/completions \
  -H "Authorization: Bearer $MISTRAL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "mistral-large-4",
    "messages": [
      {
        "role": "user",
        "content": "Our API returns 200 with an empty body under load. List likely causes in order of probability."
      }
    ],
    "reasoning_effort": "high"
  }'
Enter fullscreen mode Exit fullscreen mode

จัดการ response แบบ chunks

เมื่อใช้ reasoning_effort: "high" ค่า message.content อาจไม่ใช่สตริง แต่เป็นรายการ chunks ซึ่งมีทั้ง:

  • chunk ประเภท thinking สำหรับร่องรอยการให้เหตุผล
  • chunk ประเภท text สำหรับคำตอบสุดท้าย

อย่าพิมพ์ response.choices[0].message.content ตรง ๆ โดยไม่ตรวจประเภทข้อมูล ให้ดึงเฉพาะ text chunk:

response = client.chat.complete(
    model="mistral-large-4",
    messages=[
        {
            "role": "user",
            "content": "Why would a 200 response have an empty body?",
        }
    ],
    reasoning_effort="high",
)

content = response.choices[0].message.content

if isinstance(content, str):
    answer = content
else:
    answer = "".join(
        chunk.text
        for chunk in content
        if chunk.type == "text"
    )

print(answer)
Enter fullscreen mode Exit fullscreen mode

thinking tokens ถูกคิดราคาเป็น output tokens ดังนั้นใช้ "none" เป็นค่าเริ่มต้น และเปิด "high" เฉพาะงานที่ต้องใช้การให้เหตุผลเพิ่มเติม

ขั้นตอนที่ 5: ส่งรูปภาพ

Large 4 เป็นโมเดลมัลติโมดัลโดยกำเนิด และมี vision encoder ขนาด 1.6 พันล้านพารามิเตอร์ ส่งรูปภาพเป็น content part ถัดจากข้อความได้:

curl https://api.mistral.ai/v1/chat/completions \
  -H "Authorization: Bearer $MISTRAL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "mistral-large-4",
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "text",
            "text": "This is a screenshot of our API error dashboard. Which endpoint is failing most and what is the error code?"
          },
          {
            "type": "image_url",
            "image_url": "https://example.com/dashboard.png"
          }
        ]
      }
    ]
  }'
Enter fullscreen mode Exit fullscreen mode

สำหรับไฟล์ในเครื่อง ให้ส่งเป็น base64 data URL:

{
  "type": "image_url",
  "image_url": "data:image/png;base64,<encoded>"
}
Enter fullscreen mode Exit fullscreen mode

Mistral รายงานว่า Large 4 ได้ 42% บน Dense 200 visual-grounding benchmark นำหน้า GPT-6 Astra ที่ 41% เล็กน้อย งานที่เหมาะสมจึงรวมถึงภาพหน้าจอแดชบอร์ด แผนภูมิ และสถานะ UI

ขั้นตอนที่ 6: ใช้ Function Calling

Function calling ช่วยให้โมเดลเลือกเรียกเครื่องมือ แล้วให้แอปพลิเคชันของคุณเป็นผู้ดำเนินการจริง Large 4 ทำคะแนน 59.9% บน AutomationBench สำหรับ agent benchmarks

เริ่มจากนิยาม tool ด้วย JSON Schema:

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_order_status",
            "description": "Look up the status of an order by its ID.",
            "parameters": {
                "type": "object",
                "properties": {
                    "order_id": {
                        "type": "string",
                        "description": "The order ID, e.g. ORD-1042",
                    }
                },
                "required": ["order_id"],
            },
        },
    }
]

messages = [
    {
        "role": "user",
        "content": "Where is order ORD-1042?",
    }
]

response = client.chat.complete(
    model="mistral-large-4",
    messages=messages,
    tools=tools,
    tool_choice="auto",
)

tool_call = response.choices[0].message.tool_calls[0]

print(tool_call.function.name)
print(tool_call.function.arguments)
Enter fullscreen mode Exit fullscreen mode

โมเดลจะเลือก tool และส่ง arguments กลับมา แต่ โค้ดของคุณต้องเป็นผู้เรียกฟังก์ชันจริงเอง จากนั้นส่งผลลัพธ์กลับด้วย tool_call_id เดียวกัน:

import json

result = {
    "order_id": "ORD-1042",
    "status": "shipped",
    "eta": "2026-10-09",
}

messages.append(response.choices[0].message)

messages.append({
    "role": "tool",
    "name": "get_order_status",
    "content": json.dumps(result),
    "tool_call_id": tool_call.id,
})

final = client.chat.complete(
    model="mistral-large-4",
    messages=messages,
    tools=tools,
)

print(final.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

schema ของ tool เป็น JSON Schema มาตรฐาน หาก API ของคุณมี OpenAPI spec อยู่แล้ว ให้นำ request schema ของแต่ละ operation มาใช้ใน parameters ได้โดยตรง การออกแบบ spec ใน Apidog ก่อนช่วยให้ schema ของ tool และ API จริงตรงกัน

ขั้นตอนที่ 7: รับ JSON ที่เครื่องอ่านได้

เมื่อต้องส่งผลลัพธ์ไปยังบริการปลายน้ำ ให้กำหนด response_format แทนการหวังว่าโมเดลจะคืน JSON จาก prompt เพียงอย่างเดียว:

curl https://api.mistral.ai/v1/chat/completions \
  -H "Authorization: Bearer $MISTRAL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "mistral-large-4",
    "messages": [
      {
        "role": "user",
        "content": "Extract method, path and status code from: GET /v1/users/42 returned 404. Reply in JSON."
      }
    ],
    "response_format": {
      "type": "json_object"
    }
  }'
Enter fullscreen mode Exit fullscreen mode

ระบุความต้องการ JSON ทั้งใน prompt และ response_format เสมอ

สำหรับโครงสร้างที่เข้มงวดกว่า Mistral ยังรองรับ:

{
  "type": "json_schema",
  "json_schema": {
    "...": "full schema here"
  }
}
Enter fullscreen mode Exit fullscreen mode

ใน Apidog ให้เพิ่ม JSON Schema assertion กับ response เพื่อให้การเปลี่ยนรูปแบบข้อมูลทำให้ test ล้มเหลวอย่างชัดเจน แทนที่จะส่งข้อมูลผิดรูปแบบไปทำให้ downstream service มีปัญหา

ค่าใช้จ่าย

การใช้งาน ราคาพรีวิว ราคาปกติ
อินพุต ต่อ 1 ล้านโทเค็น $0.68 $1.36
อินพุตแคช ต่อ 1 ล้านโทเค็น $0.07 $0.14
เอาต์พุต ต่อ 1 ล้านโทเค็น $2.09 $4.18

ตัวอย่าง: เอเจนต์เรียกใช้งาน 10,000 ครั้งต่อวัน โดยแต่ละครั้งใช้ 3,000 input tokens และ 500 output tokens

  • อินพุตรวม: 30 ล้านโทเค็น
  • หาก 2,500 จาก 3,000 โทเค็นเป็น cache:
    • 25 ล้าน cached tokens ที่ $0.07 = $1.75/วัน
    • 5 ล้าน input tokens ใหม่ที่ $0.68 = $3.40/วัน
    • รวม input = ประมาณ $5.15/วัน
  • เอาต์พุต: 5 ล้านโทเค็นที่ $2.09 = ประมาณ $10.45/วัน
  • รวม = ประมาณ $15.60/วันในราคาพรีวิว หรือประมาณ $31 ในราคาปกติ

ปริมาณงานเดียวกันบน GPT-6 Astra ที่ราคา $10 / $50 ต่อ 1 ล้านโทเค็นก่อนหักส่วนลด cache จะมีต้นทุนหลายร้อยดอลลาร์ต่อวัน Mistral ยังไม่ได้ระบุวันสิ้นสุดราคาพรีวิว ดังนั้นควรวางงบโดยอิงราคาปกติ

ข้อผิดพลาดที่พบบ่อย

ข้อผิดพลาด สาเหตุที่เป็นไปได้ วิธีแก้ไข
401 Unauthorized ไม่มี key หรือ key ไม่ถูกต้อง ตรวจสอบ echo $MISTRAL_API_KEY และ prefix Bearer
404 / โมเดลไม่ถูกต้อง พิมพ์ Model ID ผิด ใช้ mistral-large-4 ให้ตรงเป๊ะ
422 Unprocessable Entity request body ไม่ถูกต้อง โดยเฉพาะ schema ของ tools ตรวจสอบ JSON Schema ใน parameters ของทุก tool
429 Too Many Requests เกิน rate limit ของ workspace รอแล้ว retry หรือเพิ่มขีดจำกัดใน Studio
คำตอบพิมพ์ออกมาเป็นรายการ reasoning_effort: "high" ส่งคืน chunks ดึงเฉพาะ chunk ประเภท text ตามขั้นตอนที่ 4

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

Mistral Large 4 เข้ากันได้กับ OpenAI หรือไม่?

รูปแบบ request ใกล้เคียงกันมาก ทั้ง model, messages, tools, tool_choice และ response_format ใช้งานตามที่คาดหวังได้ อย่างไรก็ตาม ควรใช้ Mistral SDK หรือ HTTP API โดยตรงเพื่อความปลอดภัย และ reasoning output ใช้รูปแบบ chunk ของ Mistral เอง

ฉันสามารถรันโมเดลบนเครื่องของตัวเองได้เมื่อใด?

Mistral ระบุว่าจะจัดส่ง weights ภายในสิ้นเดือนตุลาคม 2026 โมเดลมีพารามิเตอร์รวม 1.05 ล้านล้านตัว จึงต้องใช้ฮาร์ดแวร์เซิร์ฟเวอร์ที่มี GPU หลายตัว ระหว่างนี้ ดูคู่มือ รัน Mistral 3 บนเครื่องของคุณ สำหรับโมเดลขนาดเล็กกว่า

พรีวิวมีความเสถียรพอสำหรับ production หรือไม่?

ยังไม่ควรถือว่าเสถียรเต็มรูปแบบ โมเดลถูกระบุเป็น public preview และอาจเปลี่ยนแปลงก่อนเปิดตัว Open Weights ควรทำสิ่งต่อไปนี้:

  1. บันทึกชุดทดสอบที่แน่นอน
  2. รัน regression test ทุกครั้งที่ Mistral อัปเดตโมเดล
  3. ตั้งค่า fallback model
  4. ตรวจสอบคุณภาพ ความหน่วง และต้นทุนเป็นระยะ

ฉันใช้ Large 4 กับโค้ด Mistral เดิมได้หรือไม่?

ได้ ใช้ Base URL, การยืนยันตัวตน และ SDK เดิม แล้วเปลี่ยนค่า model เป็น mistral-large-4 หากย้ายมาจาก Medium 3.5 โปรดดู คู่มือ Mistral Medium 3.5 API สำหรับส่วนที่ยังใช้งานเหมือนเดิม

สรุป

เริ่มต้นด้วย request เดียวให้ผ่านก่อน จากนั้นนำ prompt จริงของคุณมารันเทียบ Large 4 กับโมเดลปัจจุบันแบบคู่ขนาน บันทึกคำขอทั้งสองใน Apidog เพิ่ม assertion สำหรับ status และรูปแบบ response แล้วคุณจะวัดได้ภายในหนึ่งวันว่า Le Chonk เหมาะกับระบบของคุณหรือไม่ โดยเฉพาะในช่วงที่ราคาพรีวิวยังลดลงครึ่งหนึ่ง

Top comments (0)