Skip to main content

Query Optimization

Use Single, Focused Queries

Start with one semantic query to minimize cost and latency. Multiple queries are expensive and should only be used for complex, multi-faceted questions.

Good

semantic_queries: [
  "How to implement authentication?"
]

Avoid

semantic_queries: [
  "auth",
  "login",
  "session",
  "cookies"
]
If one query doesn’t return enough results, make multiple separate search calls instead of adding more queries to a single call.

Combine Semantic + Regex

Use natural language for semantic search, then filter with precise regex patterns for the best results.
Example
{
  semantic_queries: ["How to handle async operations?"],
  pattern: "async\\s+(function|def|fn)"
}
This approach leverages:
  • Semantic search: Finds conceptually relevant documentation
  • Regex filtering: Narrows results to specific code patterns

Pattern Writing Tips

Case Sensitivity

By default, patterns are case-sensitive. Use (?i) prefix for case-insensitive matching.
authentication
Examples:
  • authentication → matches “authentication” only
  • (?i)authentication → matches “Authentication”, “AUTHENTICATION”, “authentication”

Common Patterns

Matches letters, digits, and underscoresExample: function\s+\w+ matches function names
Matches spaces, tabs, newlinesExample: async\s+def matches async function definitions
Matches any character (greedy)Example: function.*{ matches from “function” to opening brace
Matches “a” OR “b”Example: GET|POST|PUT|DELETE matches HTTP methods
Matches any single character in the setExample: [Aa]uth matches “auth” or “Auth”

Escaping Special Characters

Escape special regex characters with a backslash:
CharacterEscapedUsage
.\.Literal dot
( )\( \)Literal parentheses
[ ]\[ \]Literal brackets
{ }\{ \}Literal braces
\\\Literal backslash
Example: To match interface{} in Go:
interface\{\}

Cost & Performance

Performance TipsThese practices help reduce costs and improve response times:
  • Cache results for frequently searched patterns
  • Use specific regex patterns to reduce result size
  • Avoid overly broad patterns that match everything
  • Start with narrow queries and broaden if needed

Cost Optimization Workflow

1

Start with Focused Query

Use a single, specific semantic query:
semantic_queries: ["How to implement middleware?"]
2

Apply Precise Pattern

Use a targeted regex pattern:
pattern: "middleware|NextRequest|NextResponse"
3

Refine if Needed

If results are insufficient, broaden the pattern or make additional calls

Pattern Specificity

Balance between too broad and too narrow:

Too Broad

.*
Matches everything, returns too many results

Too Narrow

function\s+authenticateUser\s*\(\s*req:\s*Request
May miss relevant variations

Just Right

(authenticate|auth)\w*\s*\(
Captures auth functions with variations

With Context

middleware.*auth|auth.*middleware
Finds auth-related middleware patterns

Real-World Scenarios

Finding API Endpoints

search_documentation({
  sdk_name: "express",
  registry: "npm",
  semantic_queries: [
    "How to create REST API routes?"
  ],
  pattern: "app\\.(get|post|put|delete|patch)"
})

Database Queries

search_documentation({
  sdk_name: "prisma",
  registry: "npm",
  semantic_queries: [
    "How to perform database queries and relations?"
  ],
  pattern: "prisma\\.(find|create|update|delete)"
})

Error Handling

search_documentation({
  sdk_name: "axios",
  registry: "npm",
  semantic_queries: [
    "How to handle HTTP errors and retries?"
  ],
  pattern: "catch|error|retry|interceptor"
})

Documentation Quality Tips

Be Specific

Ask focused questions about specific features or patterns

Use Keywords

Include relevant technical terms in your semantic queries

Test Patterns

Validate regex patterns at regex101.com before using

Iterate

Start broad, then refine based on initial results

Next Steps