---
title: "What Is an Embedding? Vectorization, 3 Uses, and a Working API Call"
description: "Embeddings turn text into numbers so similar meaning means small distance. Learn vectors, cosine similarity, three uses, and a runnable API call."
url: https://cdnping.org/en/blog/what-is-embedding-beginner
language: en
published: 2026-11-25
category: "AI concepts"
tags: ["Embedding","Vectorization","Semantic search","AI API","Beginner"]
source: cdnping.org
license: Free to quote and summarize with a link to the url above.
---
# What Is an Embedding? Vectorization, 3 Uses, and a Working API Call

What is an embedding? Put simply, it converts a piece of text (or an image, and so on) into a long list of numbers called a vector. Text with similar meaning produces vectors that sit close together in space, so a program can tell whether two sentences are alike by computing a distance, instead of matching keywords character by character.

This article goes in a beginner's order: what a vector is, how to compare vectors, what you can do with them, how to make one API call, and a few common pitfalls.

## What does an embedding actually turn text into?

An embedding model takes a piece of text and returns an array of fixed length, for example a few hundred to a few thousand floating-point numbers. The exact length depends on the model; check the model's official documentation.

Think of it as coordinates. With only two dimensions, "cat" and "kitten" would land near each other, while "stock market" would be far away. Real vectors have many more dimensions, and a person can't tell what each one stands for, but the distance relationships are meaningful.

Two things to remember up front:

- Only vectors produced by the same model can be compared with each other. If you switch models, you have to regenerate vectors for all the text in your store.
- A vector only represents a position in meaning. It can't be turned back into the original text, and it doesn't answer questions. It is raw material for retrieval and comparison.

## How do you compare two vectors?

The most common measure is cosine similarity: it looks at the angle between two vectors, and the closer the value is to 1, the more similar they are. Many vector databases also support dot product or Euclidean distance. Whether you need to normalize first depends on the model and the vector database, so check their documentation.

Computing cosine similarity takes only a few lines. This Python needs no libraries:

```python
import math

def cosine(a, b):
    dot = sum(x * y for x, y in zip(a, b))
    na = math.sqrt(sum(x * x for x in a))
    nb = math.sqrt(sum(y * y for y in b))
    return dot / (na * nb)
```

There is no universal standard for the absolute value of a similarity score, and the distribution differs between models. To decide what score counts as "relevant," sample your own data. Don't copy someone else's threshold.

## Three common uses for embeddings

**Semantic search.** Split documents into passages and store a vector for each one. When a user asks a question, convert the question into a vector too and find the closest few passages. This is the first step of many "knowledge base Q&A" systems; the passages that are found are then handed to a chat model to produce the answer.

**Deduplication and clustering.** Two texts worded differently but meaning roughly the same will have a small vector distance. You can use this to merge duplicate support tickets or similar reviews, or to group a large batch of text by topic.

**Classification.** Prepare a few examples for each category, and assign new text to the category whose examples it is closest to. You can also use the vectors as features and train an ordinary classifier on them.

What these three uses have in common is that the model doesn't need to generate anything, only to compare, so it is usually cheaper and faster than calling a chat model. For actual prices, see the provider's official page.

## How do you call an embedding API once?

Many services offer an embeddings endpoint compatible with the OpenAI format. The request body is mainly the model name and the text to convert. Here is an example; replace the endpoint, key, and model name with the values from your provider's documentation:

```bash
curl "$BASE_URL/embeddings" \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "<embedding model name>",
    "input": ["今天天气不错", "今天适合出门散步", "数据库连接超时怎么办"]
  }'
```

(The three sample inputs are Chinese sentences: "The weather is nice today," "Today is a good day for a walk," and "What to do about a database connection timeout.")

In the returned JSON, `data` is an array with one element per input, and the `embedding` field in each element is the vector. Passing an array in `input` converts several texts at once; the limits on item count and total length per request are in the official documentation.

Once you have the vectors, compare them with the `cosine` function above: the first two sentences should be closer to each other than either is to the third.

### Checklist to follow

1. Create an API key in your provider's console and confirm it supports embedding models. For key basics, see [What is an API key? 5 things beginners should know before their first AI API call](/en/blog/what-is-api-key-beginner).
2. Find the endpoint address (base URL). If you're not sure what that is, read [What is a base URL? One diagram to understand the "API address" and why it can be swapped](/en/blog/what-is-base-url-api-endpoint).
3. Start with 3 short texts and confirm the returned vector length matches the documentation.
4. Record the model name you used. Any text you add later must use the same one.
5. Then process your documents in bulk. Watch batch size and rate limits, and make sure failed batches can be retried.

If this is your first call to any LLM API, [How to use an LLM API: from getting a key to your first reply](/en/blog/llm-api-first-request-guide) is more complete. Embeddings are billed by token in the same way; see [What is a token in an LLM? Why are you billed per token?](/en/blog/what-is-token-in-llm).

## Common beginner pitfalls

- **Chunks that are too long or too short.** Too long, and one vector has to summarize many topics, so retrieval gets imprecise. Too short, and a single chunk lacks context. Start with natural paragraphs, then adjust based on retrieval results.
- **Mixing models.** If the store holds vectors from model A and queries use model B, the results are meaningless.
- **Treating similarity as fact.** Retrieved content is only "possibly relevant" and needs to be checked by a person or a later step.
- **Forgetting language differences.** Results for Chinese, English, and mixed-language text vary by model. A small test on your own data is the most reliable check.

## What to watch for when calling from mainland China

Embedding work means many batched requests, so network latency and stability directly affect how long a whole job takes. You can first use the [cdnping online speed test](/en) to see the connection time and round trip from your network to each provider's API; for background, see [High latency reaching AI APIs from China: how to troubleshoot](/en/learn/ai-api-latency-china). If you are choosing a provider, the [model monitoring](/en/models) page shows availability and latency for providers on OpenRouter's public API, which can serve as a reference.

## Don't want to sign up with each provider separately?

If you mainly use tools such as Claude Code or Codex and can't easily get an overseas credit card, you can look at AGI Plan. It is a multi-provider LLM aggregation gateway: a monthly subscription paid in RMB, with no overseas credit card needed. One quota can be switched between several capability groups, and you keep using your existing tools by changing just one API address. AGI Plan is a commercial partner of cdnping. The service states that it is not an officially authorized channel and that some models are available only outside mainland China. Whether it offers the embedding capability you need, and which models are available, is determined by the [AGI Plan models page](https://agiplan.dev/models). For first steps, see the [AGI Plan beginner guide](https://agiplan.dev/startup).

## Next step

Don't rush to build a full knowledge base. Take 10 of your own texts, make one API call by following the checklist above, and use `cosine` to compute the pairwise similarities. See whether the results match your intuition. Once you can explain that step, connecting a vector database and a chat model will go much more smoothly. To see what API formats have in common, read [What does "OpenAI-compatible API" mean? Why do so many tools only need an address and a key?](/en/blog/openai-compatible-api-explained).
