Embeddings
Create vector embeddings using the POST /v1/embeddings endpoint.
Endpoint
POST https://api.allroutes.ai/v1/embeddings
Authentication
Authorization: Bearer allroutes_sk_...
Content-Type: application/json
Request Body
| Parameter | Type | Required | Description |
|---|---|---|---|
model | string | Yes | Embedding model identifier (e.g., text-embedding-3-small, text-embedding-3-large) |
input | string or array | Yes | Text to embed. A single string or an array of strings for batch embedding. |
encoding_format | string | No | Output format: "float" (default) or "base64" |
dimensions | integer | No | Truncate embeddings to this many dimensions (supported models only) |
Request Examples
Single Input
curl https://api.allroutes.ai/v1/embeddings \
-H "Authorization: Bearer allroutes_sk_..." \
-H "Content-Type: application/json" \
-d '{
"model": "text-embedding-3-small",
"input": "The quick brown fox jumps over the lazy dog"
}'
Batch Input
curl https://api.allroutes.ai/v1/embeddings \
-H "Authorization: Bearer allroutes_sk_..." \
-H "Content-Type: application/json" \
-d '{
"model": "text-embedding-3-small",
"input": [
"First sentence to embed",
"Second sentence to embed",
"Third sentence to embed"
]
}'
Response Format
{
"object": "list",
"data": [
{
"object": "embedding",
"index": 0,
"embedding": [0.0023064255, -0.009327292, 0.015797347, ...]
}
],
"model": "text-embedding-3-small",
"usage": {
"prompt_tokens": 9,
"total_tokens": 9
}
}
Response Fields
| Field | Type | Description |
|---|---|---|
data | array | Array of embedding objects |
data[].embedding | array of floats | The embedding vector |
data[].index | integer | Position of the input in the request array |
model | string | Model used to generate the embeddings |
usage.prompt_tokens | integer | Number of tokens consumed |
usage.total_tokens | integer | Total tokens (same as prompt_tokens for embeddings) |
SDK Examples
Python
from allroutes import AllRoutesClient
client = AllRoutesClient(api_key="allroutes_sk_...")
# Single input
resp = client.create_embedding(
model="text-embedding-3-small",
input="The quick brown fox jumps over the lazy dog",
)
vector = resp["data"][0]["embedding"]
print(f"Dimensions: {len(vector)}")
# Batch input
resp = client.create_embedding(
model="text-embedding-3-small",
input=["First sentence", "Second sentence", "Third sentence"],
)
for item in resp["data"]:
print(f"Index {item['index']}: {len(item['embedding'])} dims")
Go
embResp, err := client.CreateEmbedding(ctx, &allroutes.EmbeddingRequest{
Model: "text-embedding-3-small",
Input: "The quick brown fox jumps over the lazy dog",
})
if err != nil {
log.Fatal(err)
}
fmt.Printf("Dimensions: %d\n", len(embResp.Data[0].Embedding))
fmt.Printf("Tokens used: %d\n", embResp.Usage.TotalTokens)
Supported Models
AllRoutes routes embedding requests to the appropriate provider based on the model name:
| Model | Provider | Dimensions | Max Input Tokens |
|---|---|---|---|
text-embedding-3-small | OpenAI | 1536 | 8191 |
text-embedding-3-large | OpenAI | 3072 | 8191 |
text-embedding-ada-002 | OpenAI | 1536 | 8191 |
embed-english-v3.0 | Cohere | 1024 | 512 |
embed-multilingual-v3.0 | Cohere | 1024 | 512 |
Use the Models API to list all available embedding models with current pricing.
Error Codes
| Status | Description |
|---|---|
400 | Invalid request (e.g., empty input, unsupported model) |
401 | Invalid or missing API key |
402 | Insufficient credits |
413 | Input exceeds the model's maximum token count |
429 | Rate limit exceeded |
500 | Provider error (automatically retried) |
Cost Optimization
Embeddings are typically cheap, but large batch jobs add up. Three high-impact tactics:
- Batch where possible -- a single request with a 100-element
inputarray beats 100 sequential requests on both latency and the per-request fixed overhead. - Use smaller dimensions --
text-embedding-3-smallis sufficient for most retrieval workloads at one-fifth the cost oftext-embedding-3-large. - Truncate dimensions -- pass
dimensions: 512on supported models to shrink vectors and downstream storage cost without retraining.
See Cost Optimization for benchmark numbers.
See Also
- Rerank API -- pair embeddings with rerankers for higher-quality retrieval
- Models API -- list all available embedding models with pricing
- Caching -- exact-match cache works for embeddings too
- Cost Optimization -- choose the cheapest model per task