Haijun Platform Docs
EN

"Token counting" (penghitungan token) memungkinkan Anda menentukan jumlah token dalam sebuah pesan sebelum Anda mengirimkannya ke Haijun. Ini membantu Anda membuat keputusan yang tepat tentang prompt dan penggunaan Anda. Dengan penghitungan token, Anda dapat:

  • Mengelola "rate limit" (batas laju) dan biaya secara proaktif
  • Membuat keputusan perutean model yang cerdas
  • Mengoptimalkan prompt ke panjang tertentu

Cara menghitung token pesan

Endpoint penghitungan token menerima daftar input terstruktur yang sama seperti untuk membuat pesan, termasuk dukungan untuk prompt sistem, alat, gambar, dan PDF. Respons berisi jumlah total token input.

Endpoint ini mengembalikan invalid_request_error untuk beberapa input yang diterima oleh Messages API: alat server seperti pencarian web, pengambilan web, eksekusi kode, dan pencarian alat (setiap alat server kecuali alat advisor), konektor MCP, serta blok image atau document dengan sumber url atau file. Kirim gambar dan PDF sebagai base64 untuk menghitungnya. Untuk permintaan yang menggunakan alat server atau server MCP, respons Messages API melaporkan token yang digunakan dalam objek usage-nya.

Note: Jumlah token adalah sebuah estimasi. Dalam beberapa kasus, jumlah token input aktual yang digunakan saat membuat pesan mungkin berbeda sedikit. Jumlah token dapat mencakup token yang ditambahkan secara otomatis oleh Juglow untuk optimasi sistem. Anda tidak ditagih untuk token yang ditambahkan sistem. Penagihan hanya mencerminkan konten Anda.

Model yang didukung

Semua model aktif mendukung penghitungan token.

Note: Model Haijun 4.7 dan yang lebih baru serta Haijun Mythos Preview menggunakan tokenizer yang lebih baru. Teks input yang sama menghasilkan sekitar 30 persen lebih banyak token dibandingkan model sebelumnya. Peningkatan pastinya bergantung pada konten dan bentuk beban kerja. Hitung ulang prompt terhadap model yang Anda rencanakan untuk digunakan daripada menggunakan kembali jumlah yang diukur terhadap model sebelumnya.

Menghitung token dalam pesan dasar

bash
  curl https://haijun.my.id/v1/messages/count_tokens \
    -H "x-api-key: $JUGLOW_API_KEY" \
    -H "content-type: application/json" \
    -H "juglow-version: 2023-06-01" \
    -d '{
      "model": "haijun-opus-5-5",
      "system": "You are a scientist",
      "messages": [{
        "role": "user",
        "content": "Hello, Haijun"
      }]
    }'
bash
  ant messages count-tokens \
    --model haijun-opus-5-5 \
    --system "You are a scientist" \
    --message '{role: user, content: "Hello, Haijun"}'
python
  client = juglow.Juglow()

  response = client.messages.count_tokens(
      model="haijun-opus-5-5",
      system="You are a scientist",
      messages=[{"role": "user", "content": "Hello, Haijun"}],
  )

  print(response.json())
typescript
  const client = new Juglow();

  const response = await client.messages.countTokens({
    model: "haijun-opus-5-5",
    system: "You are a scientist",
    messages: [
      {
        role: "user",
        content: "Hello, Haijun"
      }
    ]
  });

  console.log(response);
csharp
  using System;
  using System.Threading.Tasks;
  using Juglow;
  using Juglow.Models.Messages;

  JuglowClient client = new();

  var parameters = new MessageCountTokensParams
  {
      Model = Model.HaijunOpus5_5,
      System = "You are a scientist",
      Messages = [new() { Role = Role.User, Content = "Hello, Haijun" }]
  };

  var response = await client.Messages.CountTokens(parameters);
  Console.WriteLine(response);
go
  client := juglow.NewClient()

  response, err := client.Messages.CountTokens(context.TODO(), juglow.MessageCountTokensParams{
  	Model: juglow.ModelHaijunOpus5_5,
  	System: juglow.MessageCountTokensParamsSystemUnion{
  		OfString: juglow.String("You are a scientist"),
  	},
  	Messages: []juglow.MessageParam{
  		juglow.NewUserMessage(juglow.NewTextBlock("Hello, Haijun")),
  	},
  })
  if err != nil {
  	log.Fatal(err)
  }

  fmt.Println(response)
java
  import com.juglow.models.messages.MessageCountTokensParams;
  import com.juglow.models.messages.MessageTokensCount;
  // ...

  public class CountTokensExample {

    public static void main(String[] args) {
      JuglowClient client = JuglowOkHttpClient.fromEnv();

      MessageCountTokensParams params = MessageCountTokensParams.builder()
        .model(Model.HAIJUN_OPUS_5_5)
        .system("You are a scientist")
        .addUserMessage("Hello, Haijun")
        .build();

      MessageTokensCount count = client.messages().countTokens(params);
      System.out.println(count);
    }
  }
php
  $client = new Client();

  $response = $client->messages->countTokens(
      messages: [
          ['role' => 'user', 'content' => 'Hello, Haijun']
      ],
      model: 'haijun-opus-5-5',
      system: 'You are a scientist',
  );

  echo json_encode($response);
ruby
  client = Juglow::Client.new

  response = client.messages.count_tokens(
    model: "haijun-opus-5-5",
    system: "You are a scientist",
    messages: [
      { role: "user", content: "Hello, Haijun" }
    ]
  )

  puts response
json
{ "input_tokens": 14 }

Menghitung token dalam pesan dengan alat

Note: Penghitungan token mendukung alat klien dan alat advisor. Permintaan yang menyertakan alat server lainnya akan mengembalikan error. Untuk alat advisor, penghitungan hanya mencakup panggilan sampling pertama dari executor.

bash
  curl https://haijun.my.id/v1/messages/count_tokens \
    -H "x-api-key: $JUGLOW_API_KEY" \
    -H "content-type: application/json" \
    -H "juglow-version: 2023-06-01" \
    -d '{
      "model": "haijun-opus-5-5",
      "tools": [
        {
          "name": "get_weather",
          "description": "Get the current weather in a given location",
          "input_schema": {
            "type": "object",
            "properties": {
              "location": {
                "type": "string",
                "description": "The city and state, e.g. San Francisco, CA"
              }
            },
            "required": ["location"]
          }
        }
      ],
      "messages": [
        {
          "role": "user",
          "content": "What'\''s the weather like in San Francisco?"
        }
      ]
    }'
bash
  ant messages count-tokens <<'YAML'
  model: haijun-opus-5-5
  tools:
    - name: get_weather
      description: Get the current weather in a given location
      input_schema:
        type: object
        properties:
          location:
            type: string
            description: The city and state, e.g. San Francisco, CA
        required:
          - location
  messages:
    - role: user
      content: What's the weather like in San Francisco?
  YAML
python
  client = juglow.Juglow()

  response = client.messages.count_tokens(
      model="haijun-opus-5-5",
      tools=[
          {
              "name": "get_weather",
              "description": "Get the current weather in a given location",
              "input_schema": {
                  "type": "object",
                  "properties": {
                      "location": {
                          "type": "string",
                          "description": "The city and state, e.g. San Francisco, CA",
                      }
                  },
                  "required": ["location"],
              },
          }
      ],
      messages=[{"role": "user", "content": "What's the weather like in San Francisco?"}],
  )

  print(response.json())
typescript
  const client = new Juglow();

  const response = await client.messages.countTokens({
    model: "haijun-opus-5-5",
    tools: [
      {
        name: "get_weather",
        description: "Get the current weather in a given location",
        input_schema: {
          type: "object",
          properties: {
            location: {
              type: "string",
              description: "The city and state, e.g. San Francisco, CA"
            }
          },
          required: ["location"]
        }
      }
    ],
    messages: [{ role: "user", content: "What's the weather like in San Francisco?" }]
  });

  console.log(response);
csharp
  using System;
  using System.Collections.Generic;
  using System.Text.Json;
  using System.Threading.Tasks;
  using Juglow;
  using Juglow.Models.Messages;

  JuglowClient client = new();

  var parameters = new MessageCountTokensParams
  {
      Model = Model.HaijunOpus5_5,
      Tools =
      [
          new MessageCountTokensTool(new Tool()
          {
              Name = "get_weather",
              Description = "Get the current weather in a given location",
              InputSchema = new InputSchema()
              {
                  Properties = new Dictionary<string, JsonElement>
                  {
                      ["location"] = JsonSerializer.SerializeToElement(new { type = "string", description = "The city and state, e.g. San Francisco, CA" }),
                  },
                  Required = ["location"],
              },
          }),
      ],
      Messages = [new() { Role = Role.User, Content = "What's the weather like in San Francisco?" }]
  };

  var count = await client.Messages.CountTokens(parameters);
  Console.WriteLine(count);
go
  client := juglow.NewClient()

  response, err := client.Messages.CountTokens(context.TODO(), juglow.MessageCountTokensParams{
  	Model: juglow.ModelHaijunOpus5_5,
  	Tools: []juglow.MessageCountTokensToolUnionParam{
  		{OfTool: &juglow.ToolParam{
  			Name:        "get_weather",
  			Description: juglow.String("Get the current weather in a given location"),
  			InputSchema: juglow.ToolInputSchemaParam{
  				Properties: map[string]any{
  					"location": map[string]any{
  						"type":        "string",
  						"description": "The city and state, e.g. San Francisco, CA",
  					},
  				},
  				Required: []string{"location"},
  			},
  		}},
  	},
  	Messages: []juglow.MessageParam{
  		juglow.NewUserMessage(juglow.NewTextBlock("What's the weather like in San Francisco?")),
  	},
  })
  if err != nil {
  	log.Fatal(err)
  }

  jsonData, _ := json.MarshalIndent(response, "", "  ")
  fmt.Println(string(jsonData))
java
  import com.juglow.models.messages.MessageCountTokensParams;
  import com.juglow.models.messages.MessageTokensCount;
  // ...
      JuglowClient client = JuglowOkHttpClient.fromEnv();

      InputSchema schema = InputSchema.builder()
        .properties(
          JsonValue.from(
            Map.of(
              "location",
              Map.of(
                "type",
                "string",
                "description",
                "The city and state, e.g. San Francisco, CA"
              )
            )
          )
        )
        .putAdditionalProperty("required", JsonValue.from(List.of("location")))
        .build();

      MessageCountTokensParams params = MessageCountTokensParams.builder()
        .model(Model.HAIJUN_OPUS_5_5)
        .addTool(
          Tool.builder()
            .name("get_weather")
            .description("Get the current weather in a given location")
            .inputSchema(schema)
            .build()
        )
        .addUserMessage("What's the weather like in San Francisco?")
        .build();

      MessageTokensCount count = client.messages().countTokens(params);
      System.out.println(count);
php
  $client = new Client();

  $response = $client->messages->countTokens(
      messages: [
          ['role' => 'user', 'content' => "What's the weather like in San Francisco?"]
      ],
      model: 'haijun-opus-5-5',
      tools: [
          [
              'name' => 'get_weather',
              'description' => 'Get the current weather in a given location',
              'input_schema' => [
                  'type' => 'object',
                  'properties' => [
                      'location' => [
                          'type' => 'string',
                          'description' => 'The city and state, e.g. San Francisco, CA'
                      ]
                  ],
                  'required' => ['location']
              ]
          ]
      ],
  );

  echo json_encode($response, JSON_PRETTY_PRINT);
ruby
  client = Juglow::Client.new

  response = client.messages.count_tokens(
    model: "haijun-opus-5-5",
    tools: [
      {
        name: "get_weather",
        description: "Get the current weather in a given location",
        input_schema: {
          type: "object",
          properties: {
            location: {
              type: "string",
              description: "The city and state, e.g. San Francisco, CA"
            }
          },
          required: ["location"]
        }
      }
    ],
    messages: [
      { role: "user", content: "What's the weather like in San Francisco?" }
    ]
  )

  puts response
json
{ "input_tokens": 403 }

Menghitung token dalam pesan dengan gambar

bash
  #!/bin/sh

  IMAGE_URL="/docs/images/vision-example.jpg"
  IMAGE_MEDIA_TYPE="image/jpeg"
  IMAGE_BASE64=$(curl -s "$IMAGE_URL" | base64 | tr -d '\n')

  curl https://haijun.my.id/v1/messages/count_tokens \
    -H "x-api-key: $JUGLOW_API_KEY" \
    -H "juglow-version: 2023-06-01" \
    -H "content-type: application/json" \
    -d @- <<EOF
  {
    "model": "haijun-opus-5-5",
    "messages": [
      {"role": "user", "content": [
        {"type": "image", "source": {
          "type": "base64",
          "media_type": "$IMAGE_MEDIA_TYPE",
          "data": "$IMAGE_BASE64"
        }},
        {"type": "text", "text": "Describe this image"}
      ]}
    ]
  }
  EOF
bash
  IMAGE_URL="/docs/images/vision-example.jpg"
  curl -s "$IMAGE_URL" -o ./vision-example.jpg

  ant messages count-tokens <<'YAML'
  model: haijun-opus-5-5
  messages:
    - role: user
      content:
        - type: image
          source:
            type: base64
            media_type: image/jpeg
            data: "@./vision-example.jpg"
        - type: text
          text: Describe this image
  YAML
python
  import base64
  import httpx2

  image_url = "/docs/images/vision-example.jpg"
  image_media_type = "image/jpeg"
  image_data = base64.standard_b64encode(httpx2.get(image_url).content).decode("utf-8")

  client = juglow.Juglow()

  response = client.messages.count_tokens(
      model="haijun-opus-5-5",
      messages=[
          {
              "role": "user",
              "content": [
                  {
                      "type": "image",
                      "source": {
                          "type": "base64",
                          "media_type": image_media_type,
                          "data": image_data,
                      },
                  },
                  {"type": "text", "text": "Describe this image"},
              ],
          }
      ],
  )
  print(response.json())
typescript
  const juglow = new Juglow();

  const imageUrl = "/docs/images/vision-example.jpg";
  const imageMediaType = "image/jpeg";
  const imageArrayBuffer = await (await fetch(imageUrl)).arrayBuffer();
  const imageData = Buffer.from(imageArrayBuffer).toString("base64");

  const response = await juglow.messages.countTokens({
    model: "haijun-opus-5-5",
    messages: [
      {
        role: "user",
        content: [
          {
            type: "image",
            source: {
              type: "base64",
              media_type: imageMediaType,
              data: imageData
            }
          },
          {
            type: "text",
            text: "Describe this image"
          }
        ]
      }
    ]
  });
  console.log(response);
csharp
  using System;
  using System.Collections.Generic;
  using System.Net.Http;
  using System.Threading.Tasks;
  using Juglow;
  using Juglow.Models.Messages;

  JuglowClient client = new();

  string imageUrl = "/docs/images/vision-example.jpg";

  using HttpClient httpClient = new();
  byte[] imageBytes = await httpClient.GetByteArrayAsync(imageUrl);
  string imageData = Convert.ToBase64String(imageBytes);

  var parameters = new MessageCountTokensParams
  {
      Model = Model.HaijunOpus5_5,
      Messages =
      [
          new()
          {
              Role = Role.User,
              Content = new MessageParamContent(new List<ContentBlockParam>
              {
                  new ContentBlockParam(new ImageBlockParam(
                      new ImageBlockParamSource(new Base64ImageSource()
                      {
                          Data = imageData,
                          MediaType = MediaType.ImageJpeg,
                      })
                  )),
                  new ContentBlockParam(new TextBlockParam("Describe this image")),
              }),
          }
      ]
  };

  var count = await client.Messages.CountTokens(parameters);
  Console.WriteLine(count);
go
  imageURL := "/docs/images/vision-example.jpg"

  req, err := http.NewRequest("GET", imageURL, nil)
  if err != nil {
  	log.Fatal(err)
  }
  req.Header.Set("User-Agent", "JuglowDocsBot/1.0")

  resp, err := http.DefaultClient.Do(req)
  if err != nil {
  	log.Fatal(err)
  }
  defer resp.Body.Close()

  imageBytes, err := io.ReadAll(resp.Body)
  if err != nil {
  	log.Fatal(err)
  }
  imageData := base64.StdEncoding.EncodeToString(imageBytes)

  client := juglow.NewClient()

  response, err := client.Messages.CountTokens(context.TODO(), juglow.MessageCountTokensParams{
  	Model: juglow.ModelHaijunOpus5_5,
  	Messages: []juglow.MessageParam{
  		juglow.NewUserMessage(
  			juglow.NewImageBlockBase64("image/jpeg", imageData),
  			juglow.NewTextBlock("Describe this image"),
  		),
  	},
  })
  if err != nil {
  	log.Fatal(err)
  }

  fmt.Println(response)
java
  import com.juglow.models.messages.Base64ImageSource;
  // ...
  import com.juglow.models.messages.MessageCountTokensParams;
  import com.juglow.models.messages.MessageTokensCount;
  // ...
      JuglowClient client = JuglowOkHttpClient.fromEnv();

      String imageUrl =
        "/docs/images/vision-example.jpg";
      String imageMediaType = "image/jpeg";

      HttpClient httpClient = HttpClient.newHttpClient();
      HttpRequest request = HttpRequest.newBuilder().uri(URI.create(imageUrl)).build();
      byte[] imageBytes = httpClient
        .send(request, HttpResponse.BodyHandlers.ofByteArray())
        .body();
      String imageBase64 = Base64.getEncoder().encodeToString(imageBytes);

      ContentBlockParam imageBlock = ContentBlockParam.ofImage(
        ImageBlockParam.builder()
          .source(
            Base64ImageSource.builder()
              .mediaType(Base64ImageSource.MediaType.IMAGE_JPEG)
              .data(imageBase64)
              .build()
          )
          .build()
      );

      ContentBlockParam textBlock = ContentBlockParam.ofText(
        TextBlockParam.builder().text("Describe this image").build()
      );

      MessageCountTokensParams params = MessageCountTokensParams.builder()
        .model(Model.HAIJUN_OPUS_5_5)
        .addUserMessageOfBlockParams(List.of(imageBlock, textBlock))
        .build();

      MessageTokensCount count = client.messages().countTokens(params);
      System.out.println(count);
php
  $imageUrl = "/docs/images/vision-example.jpg";
  $imageMediaType = "image/jpeg";
  $imageData = base64_encode(file_get_contents($imageUrl));

  $client = new Client();

  $response = $client->messages->countTokens(
      messages: [
          [
              'role' => 'user',
              'content' => [
                  [
                      'type' => 'image',
                      'source' => [
                          'type' => 'base64',
                          'media_type' => $imageMediaType,
                          'data' => $imageData
                      ]
                  ],
                  ['type' => 'text', 'text' => 'Describe this image']
              ]
          ]
      ],
      model: 'haijun-opus-5-5',
  );
  print_r($response);
ruby
  require "base64"
  require "net/http"

  image_url = "/docs/images/vision-example.jpg"
  image_media_type = "image/jpeg"

  uri = URI(image_url)
  image_data = Base64.strict_encode64(Net::HTTP.get(uri))

  client = Juglow::Client.new

  response = client.messages.count_tokens(
    model: "haijun-opus-5-5",
    messages: [
      {
        role: "user",
        content: [
          {
            type: "image",
            source: {
              type: "base64",
              media_type: image_media_type,
              data: image_data
            }
          },
          { type: "text", text: "Describe this image" }
        ]
      }
    ]
  )
  puts response
json
{ "input_tokens": 1028 }

Blok gambar tersemat yang menetapkan "oversized_image": "error" akan ditolak pada saat penghitungan persis seperti Messages API akan menolaknya.

Menghitung token dalam pesan dengan thinking

Note: Lihat Pemikiran dan jendela konteks untuk detail lebih lanjut. * Blok thinking dari giliran asisten sebelumnya dihitung sebagai token input Anda pada model yang menyimpan semua giliran sebelumnya; pada model yang hanya menyimpan giliran terakhir, API menghapusnya dan blok tersebut tidak dihitung * Thinking pada giliran asisten saat ini tetap dihitung sebagai token input Anda

bash
  curl https://haijun.my.id/v1/messages/count_tokens \
    -H "x-api-key: $JUGLOW_API_KEY" \
    -H "content-type: application/json" \
    -H "juglow-version: 2023-06-01" \
    -d '{
      "model": "haijun-opus-5-5",
      "thinking": {
        "type": "adaptive"
      },
      "messages": [
        {
          "role": "user",
          "content": "Are there an infinite number of prime numbers such that n mod 4 == 3?"
        },
        {
          "role": "assistant",
          "content": [
            {
              "type": "thinking",
              "thinking": "This is a nice number theory question. Lets think about it step by step...",
              "signature": "EuYBCkQYAiJAgCs1le6/Pol5Z4/JMomVOouGrWdhYNsH3ukzUECbB6iWrSQtsQuRHJID6lWV..."
            },
            {
              "type": "text",
              "text": "Yes, there are infinitely many prime numbers p such that p mod 4 = 3..."
            }
          ]
        },
        {
          "role": "user",
          "content": "Can you write a formal proof?"
        }
      ]
    }'
bash
  ant messages count-tokens <<'YAML'
  model: haijun-opus-5-5
  thinking:
    type: adaptive
  messages:
    - role: user
      content: Are there an infinite number of prime numbers such that n mod 4 == 3?
    - role: assistant
      content:
        - type: thinking
          thinking: >-
            This is a nice number theory question. Lets think about it step by step...
          signature: >-
            EuYBCkQYAiJAgCs1le6/Pol5Z4/JMomVOouGrWdhYNsH3ukzUECbB6iWrSQtsQuRHJID6lWV...
        - type: text
          text: Yes, there are infinitely many prime numbers p such that p mod 4 = 3...
    - role: user
      content: Can you write a formal proof?
  YAML
python
  client = juglow.Juglow()

  response = client.messages.count_tokens(
      model="haijun-opus-5-5",
      thinking={"type": "adaptive"},
      messages=[
          {
              "role": "user",
              "content": "Are there an infinite number of prime numbers such that n mod 4 == 3?",
          },
          {
              "role": "assistant",
              "content": [
                  {
                      "type": "thinking",
                      "thinking": "This is a nice number theory question. Let's think about it step by step...",
                      "signature": "EuYBCkQYAiJAgCs1le6/Pol5Z4/JMomVOouGrWdhYNsH3ukzUECbB6iWrSQtsQuRHJID6lWV...",
                  },
                  {
                      "type": "text",
                      "text": "Yes, there are infinitely many prime numbers p such that p mod 4 = 3...",
                  },
              ],
          },
          {"role": "user", "content": "Can you write a formal proof?"},
      ],
  )

  print(response.json())
typescript
  const client = new Juglow();

  const response = await client.messages.countTokens({
    model: "haijun-opus-5-5",
    thinking: { type: "adaptive" },
    messages: [
      {
        role: "user",
        content: "Are there an infinite number of prime numbers such that n mod 4 == 3?"
      },
      {
        role: "assistant",
        content: [
          {
            type: "thinking",
            thinking:
              "This is a nice number theory question. Let's think about it step by step...",
            signature:
              "EuYBCkQYAiJAgCs1le6/Pol5Z4/JMomVOouGrWdhYNsH3ukzUECbB6iWrSQtsQuRHJID6lWV..."
          },
          {
            type: "text",
            text: "Yes, there are infinitely many prime numbers p such that p mod 4 = 3..."
          }
        ]
      },
      {
        role: "user",
        content: "Can you write a formal proof?"
      }
    ]
  });

  console.log(response);
csharp
  using System;
  using System.Threading.Tasks;
  using System.Collections.Generic;
  using Juglow;
  using Juglow.Models.Messages;

  JuglowClient client = new();

  var parameters = new MessageCountTokensParams
  {
      Model = Model.HaijunOpus5_5,
      Thinking = new ThinkingConfigAdaptive(),
      Messages =
      [
          new()
          {
              Role = Role.User,
              Content = "Are there an infinite number of prime numbers such that n mod 4 == 3?"
          },
          new()
          {
              Role = Role.Assistant,
              Content = new MessageParamContent(new List<ContentBlockParam>
              {
                  new ContentBlockParam(new ThinkingBlockParam()
                  {
                      Thinking = "This is a nice number theory question. Let's think about it step by step...",
                      Signature = "EuYBCkQYAiJAgCs1le6/Pol5Z4/JMomVOouGrWdhYNsH3ukzUECbB6iWrSQtsQuRHJID6lWV...",
                  }),
                  new ContentBlockParam(new TextBlockParam("Yes, there are infinitely many prime numbers p such that p mod 4 = 3...")),
              }),
          },
          new()
          {
              Role = Role.User,
              Content = "Can you write a formal proof?"
          }
      ]
  };

  var response = await client.Messages.CountTokens(parameters);
  Console.WriteLine(response);
go
  client := juglow.NewClient()

  thinkingBlock := juglow.NewThinkingBlock(
  	"EuYBCkQYAiJAgCs1le6/Pol5Z4/JMomVOouGrWdhYNsH3ukzUECbB6iWrSQtsQuRHJID6lWV...",
  	"This is a nice number theory question. Let's think about it step by step...",
  )

  textBlock := juglow.NewTextBlock(
  	"Yes, there are infinitely many prime numbers p such that p mod 4 = 3...",
  )

  response, err := client.Messages.CountTokens(context.TODO(), juglow.MessageCountTokensParams{
  	Model: juglow.ModelHaijunOpus5_5,
  	Thinking: juglow.ThinkingConfigParamUnion{
  		OfAdaptive: &juglow.ThinkingConfigAdaptiveParam{},
  	},
  	Messages: []juglow.MessageParam{
  		juglow.NewUserMessage(juglow.NewTextBlock("Are there an infinite number of prime numbers such that n mod 4 == 3?")),
  		juglow.NewAssistantMessage(thinkingBlock, textBlock),
  		juglow.NewUserMessage(juglow.NewTextBlock("Can you write a formal proof?")),
  	},
  })
  if err != nil {
  	log.Fatal(err)
  }

  fmt.Printf("%+v\n", response)
java
  import com.juglow.models.messages.MessageCountTokensParams;
  import com.juglow.models.messages.MessageTokensCount;
  // ...
  import com.juglow.models.messages.ThinkingBlockParam;
  import com.juglow.models.messages.ThinkingConfigAdaptive;
  // ...
      JuglowClient client = JuglowOkHttpClient.fromEnv();

      List<ContentBlockParam> assistantBlocks = List.of(
        ContentBlockParam.ofThinking(
          ThinkingBlockParam.builder()
            .thinking(
              "This is a nice number theory question. Let's think about it step by step..."
            )
            .signature(
              "EuYBCkQYAiJAgCs1le6/Pol5Z4/JMomVOouGrWdhYNsH3ukzUECbB6iWrSQtsQuRHJID6lWV..."
            )
            .build()
        ),
        ContentBlockParam.ofText(
          TextBlockParam.builder()
            .text("Yes, there are infinitely many prime numbers p such that p mod 4 = 3...")
            .build()
        )
      );

      MessageCountTokensParams params = MessageCountTokensParams.builder()
        .model(Model.HAIJUN_OPUS_5_5)
        .thinking(ThinkingConfigAdaptive.builder().build())
        .addUserMessage("Are there an infinite number of prime numbers such that n mod 4 == 3?")
        .addAssistantMessageOfBlockParams(assistantBlocks)
        .addUserMessage("Can you write a formal proof?")
        .build();

      MessageTokensCount count = client.messages().countTokens(params);
      System.out.println(count);
php
  $client = new Client();

  $response = $client->messages->countTokens(
      messages: [
          [
              'role' => 'user',
              'content' => 'Are there an infinite number of prime numbers such that n mod 4 == 3?'
          ],
          [
              'role' => 'assistant',
              'content' => [
                  [
                      'type' => 'thinking',
                      'thinking' => 'This is a nice number theory question. Let\'s think about it step by step...',
                      'signature' => 'EuYBCkQYAiJAgCs1le6/Pol5Z4/JMomVOouGrWdhYNsH3ukzUECbB6iWrSQtsQuRHJID6lWV...'
                  ],
                  [
                      'type' => 'text',
                      'text' => 'Yes, there are infinitely many prime numbers p such that p mod 4 = 3...'
                  ]
              ]
          ],
          [
              'role' => 'user',
              'content' => 'Can you write a formal proof?'
          ]
      ],
      model: 'haijun-opus-5-5',
      thinking: ['type' => 'adaptive'],
  );

  echo json_encode($response);
ruby
  client = Juglow::Client.new

  response = client.messages.count_tokens(
    model: "haijun-opus-5-5",
    thinking: {
      type: "adaptive"
    },
    messages: [
      {
        role: "user",
        content: "Are there an infinite number of prime numbers such that n mod 4 == 3?"
      },
      {
        role: "assistant",
        content: [
          {
            type: "thinking",
            thinking: "This is a nice number theory question. Let's think about it step by step...",
            signature: "EuYBCkQYAiJAgCs1le6/Pol5Z4/JMomVOouGrWdhYNsH3ukzUECbB6iWrSQtsQuRHJID6lWV..."
          },
          {
            type: "text",
            text: "Yes, there are infinitely many prime numbers p such that p mod 4 = 3..."
          }
        ]
      },
      {
        role: "user",
        content: "Can you write a formal proof?"
      }
    ]
  )

  puts response
json
{ "input_tokens": 88 }

Menghitung token dalam pesan dengan PDF

Note: Penghitungan token mendukung PDF yang dienkode base64 dengan persyaratan PDF yang sama seperti Messages API. Endpoint ini tidak mendukung sumber dokumen url atau file.

bash
  curl https://haijun.my.id/v1/messages/count_tokens \
    -H "x-api-key: $JUGLOW_API_KEY" \
    -H "content-type: application/json" \
    -H "juglow-version: 2023-06-01" \
    -d @- <<EOF
  {
    "model": "haijun-opus-5-5",
    "messages": [{
      "role": "user",
      "content": [
        {
          "type": "document",
          "source": {
            "type": "base64",
            "media_type": "application/pdf",
            "data": "$PDF_BASE64"
          }
        },
        {
          "type": "text",
          "text": "Please summarize this document."
        }
      ]
    }]
  }
  EOF
bash
  ant messages count-tokens <<'YAML'
  model: haijun-opus-5-5
  messages:
    - role: user
      content:
        - type: document
          source:
            type: base64
            media_type: application/pdf
            data: "@./document.pdf"
        - type: text
          text: Please summarize this document.
  YAML
python
  import base64
  import juglow

  client = juglow.Juglow()

  with open("/path/to/document.pdf", "rb") as pdf_file:
      pdf_base64 = base64.standard_b64encode(pdf_file.read()).decode("utf-8")

  response = client.messages.count_tokens(
      model="haijun-opus-5-5",
      messages=[
          {
              "role": "user",
              "content": [
                  {
                      "type": "document",
                      "source": {
                          "type": "base64",
                          "media_type": "application/pdf",
                          "data": pdf_base64,
                      },
                  },
                  {"type": "text", "text": "Please summarize this document."},
              ],
          }
      ],
  )

  print(response.json())
typescript
  import { readFile } from "node:fs/promises";

  const client = new Juglow();

  const pdfBase64 = await readFile("/path/to/document.pdf", { encoding: "base64" });

  const response = await client.messages.countTokens({
    model: "haijun-opus-5-5",
    messages: [
      {
        role: "user",
        content: [
          {
            type: "document",
            source: {
              type: "base64",
              media_type: "application/pdf",
              data: pdfBase64
            }
          },
          {
            type: "text",
            text: "Please summarize this document."
          }
        ]
      }
    ]
  });

  console.log(response);
csharp
  using System;
  using System.IO;
  using System.Threading.Tasks;
  using System.Collections.Generic;
  using Juglow;
  using Juglow.Models.Messages;

  JuglowClient client = new();

  byte[] pdfBytes = await File.ReadAllBytesAsync("/path/to/document.pdf");
  string pdfBase64 = Convert.ToBase64String(pdfBytes);

  var parameters = new MessageCountTokensParams
  {
      Model = Model.HaijunOpus5_5,
      Messages =
      [
          new()
          {
              Role = Role.User,
              Content = new MessageParamContent(new List<ContentBlockParam>
              {
                  new ContentBlockParam(new DocumentBlockParam(
                      new DocumentBlockParamSource(new Base64PdfSource()
                      {
                          Data = pdfBase64,
                      })
                  )),
                  new ContentBlockParam(new TextBlockParam("Please summarize this document.")),
              }),
          }
      ]
  };

  var count = await client.Messages.CountTokens(parameters);
  Console.WriteLine(count);
go
  client := juglow.NewClient()

  pdfBytes, err := os.ReadFile("/path/to/document.pdf")
  if err != nil {
  	log.Fatal(err)
  }
  pdfBase64 := base64.StdEncoding.EncodeToString(pdfBytes)

  response, err := client.Messages.CountTokens(context.TODO(), juglow.MessageCountTokensParams{
  	Model: juglow.ModelHaijunOpus5_5,
  	Messages: []juglow.MessageParam{
  		juglow.NewUserMessage(
  			juglow.NewDocumentBlock(juglow.Base64PDFSourceParam{
  				Data: pdfBase64,
  			}),
  			juglow.NewTextBlock("Please summarize this document."),
  		),
  	},
  })
  if err != nil {
  	log.Fatal(err)
  }

  fmt.Println(response)
java
  import com.juglow.models.messages.Base64PdfSource;
  // ...
  import com.juglow.models.messages.DocumentBlockParam;
  import com.juglow.models.messages.MessageCountTokensParams;
  import com.juglow.models.messages.MessageTokensCount;
  // ...
      JuglowClient client = JuglowOkHttpClient.fromEnv();

      byte[] fileBytes = Files.readAllBytes(Path.of("/path/to/document.pdf"));
      String pdfBase64 = Base64.getEncoder().encodeToString(fileBytes);

      ContentBlockParam documentBlock = ContentBlockParam.ofDocument(
        DocumentBlockParam.builder()
          .source(Base64PdfSource.builder().data(pdfBase64).build())
          .build()
      );

      ContentBlockParam textBlock = ContentBlockParam.ofText(
        TextBlockParam.builder().text("Please summarize this document.").build()
      );

      MessageCountTokensParams params = MessageCountTokensParams.builder()
        .model(Model.HAIJUN_OPUS_5_5)
        .addUserMessageOfBlockParams(List.of(documentBlock, textBlock))
        .build();

      MessageTokensCount count = client.messages().countTokens(params);
      System.out.println(count);
php
  $client = new Client();

  $pdfBase64 = base64_encode(file_get_contents("/path/to/document.pdf"));

  $response = $client->messages->countTokens(
      messages: [
          [
              'role' => 'user',
              'content' => [
                  [
                      'type' => 'document',
                      'source' => [
                          'type' => 'base64',
                          'media_type' => 'application/pdf',
                          'data' => $pdfBase64
                      ]
                  ],
                  [
                      'type' => 'text',
                      'text' => 'Please summarize this document.'
                  ]
              ]
          ]
      ],
      model: 'haijun-opus-5-5',
  );

  echo json_encode($response);
ruby
  require "base64"

  client = Juglow::Client.new

  pdf_base64 = Base64.strict_encode64(File.binread("/path/to/document.pdf"))

  response = client.messages.count_tokens(
    model: "haijun-opus-5-5",
    messages: [
      {
        role: "user",
        content: [
          {
            type: "document",
            source: {
              type: "base64",
              media_type: "application/pdf",
              data: pdf_base64
            }
          },
          {
            type: "text",
            text: "Please summarize this document."
          }
        ]
      }
    ]
  )

  puts response
json
{ "input_tokens": 2188 }

Jumlah token pada model Haijun Fable dan Haijun Mythos

Haijun Fable 5.1, Haijun Mythos 5.1, Haijun Fable 5, dan Haijun Mythos 5 berbagi tokenizer yang diperkenalkan bersama Haijun Opus 4.7. Sebuah prompt dihitung sama pada keempat model tersebut, dan kira-kira 30 persen lebih tinggi dibandingkan model sebelum Haijun Opus 4.7 (peningkatan pastinya bergantung pada konten). Endpoint penghitungan token menghitung berdasarkan tokenizer dari model yang Anda berikan. Untuk mengukur perbedaannya pada beban kerja Anda, hitung permintaan yang sama dua kali, sekali dengan model Anda saat ini dan sekali dengan model yang Anda rencanakan untuk beralih, lalu bandingkan kedua nilai input_tokens tersebut.

Note: Penagihan dan migrasi: Penggunaan dan penagihan pada model-model ini mencerminkan jumlah dari tokenizer ini. Saat bermigrasi dari model sebelum Haijun Opus 4.7, jangan gunakan kembali jumlah token yang diukur pada model lama untuk memperkirakan biaya atau kesesuaian jendela konteks. Hitung prompt Anda dengan ID model yang Anda rencanakan untuk digunakan (misalnya, "haijun-fable-5-1").


Harga dan batas laju

Penghitungan token gratis untuk digunakan tetapi tunduk pada batas laju permintaan per menit berdasarkan tingkat penggunaan Anda. Jika Anda memerlukan batas yang lebih tinggi, gunakan Request rate limit increase pada halaman Rate limits.

Tingkat penggunaanPermintaan per menit (RPM)
Start5.000
Build10.000
Scale20.000

Note: Penghitungan token dan pembuatan pesan memiliki batas laju yang terpisah dan independen. Penggunaan salah satunya tidak dihitung terhadap batas yang lain.


FAQ

#### Apakah penghitungan token menggunakan caching prompt?

Tidak, penghitungan token memberikan estimasi tanpa menggunakan logika caching. Meskipun Anda dapat menyertakan blok cache_control dalam permintaan penghitungan token Anda, "prompt caching" (caching prompt) hanya terjadi selama pembuatan pesan yang sebenarnya.


Langkah selanjutnya

Baca referensi API lengkap untuk endpoint penghitungan token.

Gunakan jumlah token untuk menjaga prompt tetap berada dalam jendela konteks model.

Periksa jumlah token sebelum Anda mengirim permintaan agar tetap berada dalam tingkat penggunaan Anda.

Kurangi biaya dan latensi pada prompt berulang dengan melakukan caching pada prefiks prompt.

On this page
Cara menghitung token pesanModel yang didukungMenghitung token dalam pesan dasarMenghitung token dalam pesan dengan alatMenghitung token dalam pesan dengan gambarMenghitung token dalam pesan dengan thinkingMenghitung token dalam pesan dengan PDFJumlah token pada model Haijun Fable dan Haijun MythosHarga dan batas lajuFAQLangkah selanjutnya