Docs

Quickstart

Rust quickstart

The siliconnetwork crate: a blocking client, Rust 1.85 or newer, with three dependencies (ureq with rustls, serde, serde_json).

1. Add the crate and set the key

The crate is not on crates.io yet. It lives in the repository at sdks/rust; depend on a checkout.

Cargo.toml
# in your project's Cargo.toml[dependencies]siliconnetwork = { path = "../siliconnetwork/sdks/rust" }# or, with access to the repository:# siliconnetwork = { git = "https://github.com/ekempinski/siliconnetwork", branch = "main" }

Set SILICON_API_KEY=sn_live_... in the environment. Client::new() reads it and SILICON_BASE_URL; Client::builder() sets them, headers, retries and the timeout in code.

2. One chat completion, with errors handled

main.rs
use siliconnetwork::{ChatCompletionRequest, ChatMessage, Client, Error}; fn main() -> Result<(), Error> {    let client = Client::new()?; // reads SILICON_API_KEY     let req = ChatCompletionRequest::new(        "llama-3.2-3b",        vec![ChatMessage::user("Say hello in one line.")],    );    match client.chat_completion(&req) {        Ok(out) => println!("{} (request {:?})", out.text(), out.meta.request_id),        Err(Error::Api(e)) if e.is_out_of_credits() => eprintln!("add balance on the Billing page: {}", e.message),        Err(Error::Api(e)) if e.is_rate_limited() => eprintln!("rate limited; retry after {:?}", e.retry_after),        Err(Error::Api(e)) => eprintln!("{} {} (request {:?})", e.status, e.message, e.request_id),        Err(e) => return Err(e), // no answer: network, TLS, timeout, or a decoding error    }    Ok(())}

3. Stream the answer

stream.rs (inside main)
let req = ChatCompletionRequest::new(    "llama-3.2-3b",    vec![ChatMessage::user("Write a haiku about silicon.")],);for chunk in client.chat_completion_stream(&req)? {    if let Some(choice) = chunk?.choices.first() {        print!("{}", choice.delta.content.as_deref().unwrap_or(""));    }}println!();

The stream also has text() and completion() for the joined text or the whole completion. Streams are not retried.

4. List the models

models.rs (inside main)
for model in client.list_models()?.data {    println!("{} {:?}", model.id, model.aliases);}

5. Errors

siliconnetwork::Error
VariantWhen
Api(ApiError)The gateway answered with an error status, or sent an error inside a stream: status, message, code, request_id, retry_after.
ComputeUnavailable(ApiError)A compute route while no compute provider is connected.
TransportNo answer: connection, TLS, timeout.
DecodeA body could not be encoded or decoded.
ConfigThe client was set up wrong, for example no API key.

Non-streaming calls retry 429 and 5xx twice, honoring Retry-After (at most 30 seconds); Client::builder().max_retries(..) changes that. The README in sdks/rust covers embeddings, rerank, compute and the generated route methods.

Silicon Network›Docs›Guides›Quickstarts by language›Rust54 items