Prerequisites
Before you begin, make sure you have:
Installed the Dedalus SDK (see Installation )
A valid API key stored in your environment variables
Create your first chat completion
Here’s a complete example to get you started with the Dedalus SDK:
import Dedalus from 'dedalus-labs' ;
const client = new Dedalus ({
apiKey: process . env . DEDALUS_API_KEY , // This is the default and can be omitted
});
const completion = await client . chat . completions . create ({
model: 'openai/gpt-5-nano' ,
messages: [
{ role: 'system' , content: 'You are Stephen Dedalus. Respond in morose Joycean malaise.' },
{ role: 'user' , content: 'Hello, how are you today?' },
],
});
console . log ( completion . id );
console . log ( completion . choices [ 0 ]. message . content );
Import and initialize
Import the Dedalus client and create a new instance. The API key is automatically loaded from your environment variables. import Dedalus from 'dedalus-labs' ;
const client = new Dedalus ();
Create a completion
Call the chat.completions.create() method with your model and messages. const completion = await client . chat . completions . create ({
model: 'openai/gpt-5-nano' ,
messages: [
{ role: 'user' , content: 'Hello!' },
],
});
Access the response
The completion object contains the model’s response and metadata. console . log ( completion . choices [ 0 ]. message . content );
console . log ( completion . usage );
Stream responses
For real-time responses, enable streaming with Server Sent Events:
import Dedalus from 'dedalus-labs' ;
const client = new Dedalus ();
const stream = await client . chat . completions . create ({
model: 'openai/gpt-5-nano' ,
stream: true ,
messages: [
{ role: 'system' , content: 'You are Stephen Dedalus. Respond in morose Joycean malaise.' },
{ role: 'user' , content: 'What do you think of artificial intelligence?' },
],
});
for await ( const chunk of stream ) {
const content = chunk . choices [ 0 ]?. delta ?. content || '' ;
process . stdout . write ( content );
}
You can cancel a stream at any time by calling stream.controller.abort() or by using break in the loop.
Working with TypeScript types
The SDK includes full TypeScript support with type definitions for all request params and response fields:
import Dedalus from 'dedalus-labs' ;
const client = new Dedalus ();
// Type-safe request parameters
const params : Dedalus . Chat . CompletionCreateParams = {
model: 'openai/gpt-5-nano' ,
messages: [
{ role: 'system' , content: 'You are Stephen Dedalus. Respond in morose Joycean malaise.' },
{ role: 'user' , content: 'Hello, how are you today?' },
],
};
// Type-safe response
const completion : Dedalus . Chat . Completion = await client . chat . completions . create ( params );
// IntelliSense and autocomplete work throughout
console . log ( completion . choices [ 0 ]. message . content );
Handle errors
The SDK provides specific error types for different failure scenarios:
import Dedalus from 'dedalus-labs' ;
const client = new Dedalus ();
try {
const completion = await client . chat . completions . create ({
model: 'openai/gpt-5-nano' ,
messages: [
{ role: 'system' , content: 'You are Stephen Dedalus. Respond in morose Joycean malaise.' },
{ role: 'user' , content: 'Hello, how are you today?' },
],
});
console . log ( completion . choices [ 0 ]. message . content );
} catch ( err ) {
if ( err instanceof Dedalus . APIError ) {
console . log ( err . status ); // 400
console . log ( err . name ); // BadRequestError
console . log ( err . headers ); // {server: 'nginx', ...}
} else {
throw err ;
}
}
Error types
The SDK provides specific error classes for different HTTP status codes:
Status Code Error Type 400 BadRequestError401 AuthenticationError403 PermissionDeniedError404 NotFoundError422 UnprocessableEntityError429 RateLimitError>=500 InternalServerErrorN/A APIConnectionError
Customize retry behavior and request timeouts:
Global configuration
Per-request configuration
import Dedalus from 'dedalus-labs' ;
const client = new Dedalus ({
maxRetries: 5 , // default is 2
timeout: 20 * 1000 , // 20 seconds (default is 60 seconds)
});
The SDK automatically retries connection errors, timeouts (408), conflicts (409), rate limits (429), and server errors (>=500) with exponential backoff.
Use additional options
You can configure organization ID, custom providers (BYOK), and other options:
import Dedalus from 'dedalus-labs' ;
const client = new Dedalus ({
apiKey: process . env . DEDALUS_API_KEY ,
organization: 'org_123' , // Optional: scope requests to an organization
provider: 'openai' , // Optional: BYOK provider name
providerKey: process . env . PROVIDER_KEY , // Optional: BYOK provider API key
providerModel: 'gpt-4' , // Optional: BYOK model identifier
});
Next steps
API Reference Explore all available methods and options
Chat Completions Learn more about chat completion options
Embeddings Generate embeddings for text
Error Handling Advanced error handling patterns