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

ParameterTypeRequiredDescription
modelstringYesEmbedding model identifier (e.g., text-embedding-3-small, text-embedding-3-large)
inputstring or arrayYesText to embed. A single string or an array of strings for batch embedding.
encoding_formatstringNoOutput format: "float" (default) or "base64"
dimensionsintegerNoTruncate 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

FieldTypeDescription
dataarrayArray of embedding objects
data[].embeddingarray of floatsThe embedding vector
data[].indexintegerPosition of the input in the request array
modelstringModel used to generate the embeddings
usage.prompt_tokensintegerNumber of tokens consumed
usage.total_tokensintegerTotal 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:

ModelProviderDimensionsMax Input Tokens
text-embedding-3-smallOpenAI15368191
text-embedding-3-largeOpenAI30728191
text-embedding-ada-002OpenAI15368191
embed-english-v3.0Cohere1024512
embed-multilingual-v3.0Cohere1024512

Use the Models API to list all available embedding models with current pricing.

Error Codes

StatusDescription
400Invalid request (e.g., empty input, unsupported model)
401Invalid or missing API key
402Insufficient credits
413Input exceeds the model's maximum token count
429Rate limit exceeded
500Provider error (automatically retried)

Cost Optimization

Embeddings are typically cheap, but large batch jobs add up. Three high-impact tactics:

  1. Batch where possible -- a single request with a 100-element input array beats 100 sequential requests on both latency and the per-request fixed overhead.
  2. Use smaller dimensions -- text-embedding-3-small is sufficient for most retrieval workloads at one-fifth the cost of text-embedding-3-large.
  3. Truncate dimensions -- pass dimensions: 512 on 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