Smritea

C# SDK

Reference for the Smritea C# SDK. Install it, create a client, and call AddAsync, SearchAsync, GetAsync and DeleteAsync.

Ask AI about this page

The C# SDK is the Smritea.Sdk NuGet package. It gives you an async SmriteaClient with four calls: AddAsync, SearchAsync, GetAsync and DeleteAsync. The package targets .NET 10.

Install

dotnet add package Smritea.Sdk

Import the namespace:

using Smritea.Sdk;

Create a client

SmriteaClient takes your API key and your app ID. You must pass both, and a null value throws ArgumentNullException. The client implements IDisposable. Create the keys in the dashboard; see Create an app and API key.

Constructor parameterTypeDefaultDescription
apiKeystringnoneThe API key, in the form smrt_<prefix>_<secret>.
appIdstringnoneThe app ID, in the form app_....
baseUrlstring?https://api-us.smritea.aiThe host for your region. See Regions.
maxRetriesint2Retries after a rate limit. 0 turns retries off.
traceIdstring?nullA trace ID to send with each call. A bad value throws ArgumentException.
httpClientHttpClient?a new clientYour own HTTP client. Set timeouts here.

The client has no timeout parameter. Set a timeout on the HttpClient you pass in. Dispose does not dispose a client you supplied.

Methods

Every method takes a CancellationToken ct = default as its last argument.

MethodReturnsDescription
AddAsync(content, opts)Task<MemoryCreationResult>Extracts and stores memories from the text.
SearchAsync(query, opts)Task<IReadOnlyList<SearchResult>>Finds memories that match the query.
GetAsync(memoryId)Task<Memory>Returns one memory by ID.
DeleteAsync(memoryId)TaskDeletes one memory by ID.

The opts argument is optional. The GetAllAsync method exists but throws NotImplementedException. Use SearchAsync to find memories.

The names Memory, MemoryCreationResult and SearchResult are the SDK's own short names for generated types. Only the SDK assembly can use these short names. Your code can name the public generated types in Smritea.Internal.Autogen.Model: MemoryResponse, CreateMemoryResponse and SearchMemoryResponse. Declare results with var. To name one, add an alias such as using SearchResult = Smritea.Internal.Autogen.Model.SearchMemoryResponse;.

Add options

Build options with the fluent methods on new AddOptions().

MethodDescription
WithScope(scope)Who and what the memory belongs to.
WithMetadata(dictionary)Values must be strings, integers or floats.
WithEventOccurredAt(string)An RFC 3339 string, such as 2026-11-05T10:00:00Z. Smritea uses it to resolve words like "yesterday" and to date the memory.
WithRelativeStanding(RelativeStanding)The importance and decay settings for the memory.

Build a RelativeStanding with new RelativeStanding(). Chain WithImportance(float), WithDecayFactor(float) and WithDecayFunction(string). The importance ranges from 0 to 1. The decay factor is 0 or higher. The decay function takes exponential, gaussian or linear. Decay affects quick_search, context_aware_search and deep_search with temporal or rrf_temporal; importance affects deep_search with rrf or rrf_temporal.

Search options

Build options with the fluent methods on new SearchOptions().

MethodDescription
WithScope(scope)Limits results to this scope.
WithSpeakerActorId(id)Who is asking. It never filters results.
WithLimit(n)Most results to return. Omit it to use the app default.
WithMethod(m)quick_search, deep_search or context_aware_search.
WithRerankerType(r)Applies to deep_search only. See Search methods and rerankers.
WithGraphDepth(n)A value from 1 to 5.
WithValidAt(t)An RFC 3339 time. Returns memories valid at that moment.
WithFromTime(t), WithToTime(t)An RFC 3339 range. Use either one alone for an open range.
WithMetadataFilter(dictionary)See Filter by metadata.

You cannot combine WithValidAt with WithFromTime or WithToTime.

Memory scope

Build a scope with new MemoryScope() and its With methods. Every field is optional. See Memory scope for the rules.

MethodDescription
WithActorId(id)The actor's ID, up to 64 characters. Set it with WithActorType.
WithActorType(type)user, agent or system.
WithActorName(name)A display name, up to 255 characters. Used by AddAsync only.
WithConversationId(id)A conversation ID, up to 64 characters.
WithSourceType(type)conversation, document or api. Defaults to api.
WithParticipantIds(list)A list of one or more IDs. Search only.

Results

SearchAsync returns a list of results. Each one has a Score and a Memory.

PropertyDescription
ScoreThe relevance score.
Memory.ContentThe memory text.
Memory.IdThe memory ID.
Memory.MetadataThe metadata you stored.
Memory.ScopeThe scope fields of the memory.

AddAsync returns a MemoryCreationResult.

PropertyDescription
MemoriesThe memories the call stored. Empty when ExplicitSkip is true.
FactsExtractedHow many facts the text held.
SkippedCountHow many facts the call skipped.
UpdatedCountHow many existing memories the call updated.
ExplicitSkipTrue when nothing was worth storing.

Exceptions

Every error derives from SmriteaException. The SDK picks the class from the HTTP status.

ExceptionHTTP statusWhen
SmriteaBadRequestException400A parameter is invalid.
SmriteaUnauthorizedException401The API key is missing or wrong.
SmriteaPaymentRequiredException402Your plan quota has run out.
SmriteaForbiddenException403The key cannot do this.
SmriteaNotFoundException404The memory does not exist.
SmriteaConflictException409The request conflicts with current state.
SmriteaUnprocessableException422The request is well formed but not allowed.
SmriteaTooManyRequestsException429You hit a rate limit.
SmriteaExceptionotherAny other failure, including 5xx.

Each exception has HTTPStatus, Code, Retryable, Body and RequestId. SmriteaTooManyRequestsException also has RetryAfter, in seconds.

The SDK retries a 429 up to maxRetries times. It never retries a 402. For every code, see Errors and codes.

Example

This program stores a memory, searches for it and handles the failures.

using Smritea.Sdk;

var client = new SmriteaClient(
    apiKey: Environment.GetEnvironmentVariable("SMRITEA_API_KEY")!,
    appId: Environment.GetEnvironmentVariable("SMRITEA_APP_ID")!);
var alice = new MemoryScope()
    .WithActorId("alice")
    .WithActorType("user");

try
{
    await client.AddAsync("Alice is vegetarian and loves hiking",
        new AddOptions().WithScope(alice));
    var results = await client.SearchAsync("What does Alice eat?",
        new SearchOptions().WithScope(alice));
    foreach (var r in results)
        Console.WriteLine($"{r.Score:F2}  {r.Memory.Content}");
}
catch (SmriteaPaymentRequiredException)
{
    Console.WriteLine("Plan quota used up (402)");
}
catch (SmriteaTooManyRequestsException ex)
{
    Console.WriteLine($"Rate limited. Retry in {ex.RetryAfter}s");
}
catch (SmriteaException ex)
{
    Console.WriteLine($"{ex.HTTPStatus} {ex.Code} {ex.RequestId}");
}

Don't see your language? Request it on GitHub. Search existing requests first and add a 👍 if yours is there. That helps the Smritea team rank requests.

Last updated on