Local Knowledge Base from Scratch: Four Steps to Connect an AI Assistant and Control Costs

    This guide explains how to build a local Markdown knowledge base with Obsidian and connect it to an AI assistant through a cloud API in four steps. It covers data boundaries, installation and test vault setup, API configuration, model selection, troubleshooting, cost control, security checks, and backup and recovery drills, with notes on when to use a local model such as Ollama for sensitive content.

    1. Define the Data Boundary First

    Notes are stored on your computer as plain Markdown text, and storage itself does not depend on a cloud service. However, when you ask questions through a cloud large language model API, the note snippets that the AI needs to read are sent to the model provider’s servers as context. During the question-and-answer process, the content that is read leaves your machine.

    Therefore, the boundary of this setup is: notes are stored locally, while inference runs in the cloud. This boundary does not equal absolute privacy. How a provider retains request logs and whether it uses data for model training depend on its terms of service and the plan you choose. Before regular use, review the provider’s data retention and training policies on its official website. This article does not constitute legal advice; the official terms prevail.

    If your notes contain client data, unpublished research, or personal private information, there are two approaches:

    1. Keep these files outside the knowledge base so the AI cannot read them.
    2. Switch to a local model, for example by running an open-source model on your machine through Ollama.

    The following table compares the two approaches:

    Comparison Cloud API Ollama local model
    Privacy Question-and-answer content is sent to the provider Data does not leave your machine
    Answer quality Usually stronger, depending on the selected model Depends on model size and quantization version
    Hardware requirements An ordinary computer is sufficient Requires enough memory; a dedicated GPU can accelerate inference
    Response speed Depends on the network and provider Depends on local compute
    Cost Billed by usage No usage fee, but electricity and hardware investment
    Use cases Public materials, non-sensitive notes Sensitive materials, offline use
    Privacy responsibility You must verify provider policies and control what you upload You must protect the local machine and backups

    2. Step 1: Install Obsidian and Create a Test Vault

    This article uses Obsidian as an example. It is free local Markdown note-taking software. Download the installer only from the official website, obsidian.md, and avoid builds from unknown sources. Before downloading, confirm that your computer’s operating system version meets the requirements on the official website.

    After installation, open Settings and go to the About page to record the current version number. You will need this version number to troubleshoot compatibility issues with plugins later.

    Next, create a test vault:

    1. Open Obsidian and choose “Create new vault.”
    2. Choose an independent directory, such as ~/Documents/KB-test. Do not directly choose the system drive or an existing directory with important notes.
    3. Create 3 to 5 test notes in the vault with ordinary text. Do not put real sensitive information in them.
    4. Confirm that the notes can be opened and edited normally.

    To enable community plugins:

    1. Go to Settings, find the “Third-party plugins” (community plugins) option, and turn off Safe mode (Restricted mode).
    2. Click “Browse” and search for the plugin you need.
    3. Before installing, verify the plugin’s author, update time, download count, source code repository, and permission description.
    4. Install and enable the plugin, then open the plugin settings page and confirm that the index directory contains only the test notes.

    Prerequisites checklist:

    • Network: You can access the Obsidian official website and the plugin marketplace. If you use a proxy, confirm that it applies to Obsidian.
    • Account: Local Obsidian use requires no account. The API service requires a provider account.
    • Plan: Confirm that your API plan has the required models enabled and understand the billing method.
    • Version: Record the Obsidian version number and plugin version numbers.
    • Rollback: Back up the vault before updates so you can revert to an older plugin or Obsidian version if incompatibility occurs.

    3. Step 2: Configure the API

    Create an API key on the provider’s official website. Follow the principle of least privilege when creating it: enable only the models you need, set a usage limit, and do not select unrelated permissions.

    The fields to fill in the plugin settings are shown below. The exact field names vary by plugin; refer to the instructions for the plugin you use.

    Field Suggested entry Notes
    Provider Select the official interface type Choosing the wrong interface type causes requests to fail
    Base URL Follow the provider’s official documentation Do not use third-party relay addresses of unknown origin
    Model name Use the model identifier in the official documentation An incorrect name returns a model-not-found error
    API Key Save it as a system environment variable Do not write it into notes or commit it to a code repository
    Maximum output length Set a small value first, such as 1024 tokens Limits the output cost per response
    Number of retrieved snippets (Top-K) Start with 3 to 5 Larger values send more context
    Timeout Set an explicit number of seconds Avoids requests hanging for a long time
    Retry count No more than 2 Avoids duplicate charges from retries

    To save the API key as an environment variable, use the following approach. On macOS, add a line to your terminal configuration file. The variable name can be customized:

    1. export KB_LLM_API_KEY="your-api-key"

    After saving, restart the terminal and Obsidian so the variable takes effect. Windows users can add a variable with the same name in “System Environment Variables.”

    Note that some plugins save the key you enter in the plugin configuration file, usually under the .obsidian/plugins/ directory in the vault. These files should not be committed to Git or placed in an automatically synced cloud drive. If the plugin supports reading environment variables, prefer the environment variable.

    After configuration, test with a simple question, such as “List the titles of the test notes.” If the result is correct, the interface, key, and index scope are working.

    4. Step 3: Select a Model

    When selecting a model, mainly compare the following four items:

    1. Answer quality: Whether it can correctly understand Chinese notes and give answers with supporting evidence.
    2. Cost: The price per million tokens, based on the provider’s current pricing table.
    3. Context length: The longest input it can receive, which determines how many note snippets can be included at once.
    4. Local fallback: Whether a corresponding local model can replace it for handling sensitive content.

    Suggested selection method: Prepare 10 questions you would ask in daily use, answer them with two models at different price levels, and record the accuracy and time taken for each answer. Choose the one whose quality meets your needs and whose cost is acceptable. Do not look only at price, and do not default to the most expensive model.

    If you need a local model, follow this process:

    1. Install Ollama from the official Ollama website.
    2. Run ollama pull model-name in the terminal. Use the model names listed in the official Ollama model library.
    3. Confirm that the service is running. The default address is usually http://localhost:11434.
    4. In the plugin, select the local interface type and enter the address above.

    Local model answer quality is affected by model size and quantization version. Start with a smaller model, confirm that your computer runs smoothly, and then decide whether to switch to a larger model.

    5. Step 4: Troubleshooting, Cost Control, Security Verification, and Backup

    1. Common error troubleshooting

    Troubleshoot in the following order to locate problems quickly:

    Symptom Possible cause Troubleshooting order
    Plugin cannot be enabled Safe mode was not disabled; the plugin is incompatible with the Obsidian version Check the Safe mode setting, then compare with the compatible version in the plugin instructions
    401 error returned Incorrect key or environment variable not effective Check the key content and variable name, restart Obsidian, and try again
    429 error returned Too many requests or quota exhausted Reduce concurrency and check quota and usage in the provider console
    Context limit exceeded Too many retrieved snippets or the note is too long Reduce the number of retrieved snippets, narrow the index scope, and split long notes
    Answers do not include certain notes The index directory is set incorrectly or the index is not updated Check the index path and rebuild the index manually
    Request fails with no error code Proxy is not effective for the plugin Check system proxy and plugin network settings, and test in another network environment

    Change only one variable at a time after each modification, and record the result so you can roll back.

    2. Cost control

    Cloud APIs bill input tokens and output tokens separately. The cost of a single question-and-answer session can be estimated with the following formula:

    Cost per query = input tokens × input unit price + output tokens × output unit price

    Input tokens include system instructions, retrieved note snippets, and your question. The more snippets retrieved, the more input tokens are used. Unit prices vary by provider and plan. This article does not provide fixed prices; use the current official pricing table.

    Ways to control costs:

    • Reduce the number of retrieved snippets and keep only content directly relevant to the question.
    • Truncate or split overly long notes to avoid sending entire documents.
    • Cache answers to repeated questions.
    • Set a monthly budget and usage alerts in the provider console.
    • Review the bill once a week, record actual usage, and compare it with your estimate.

    3. Security verification checklist

    Before completing deployment, confirm each item:

    • The API key is not written into notes, code, or synced directories.
    • Folders containing sensitive content are excluded from the index.
    • You have verified the data retention period and training policy on the provider’s official website.
    • You have verified the plugin author and source code origin, and installed only necessary plugins.
    • Plugin configuration files are not committed to Git or placed in an automatically synced cloud drive.
    • If question-and-answer logs need to be shared, remove sensitive content first.
    • Rotate API keys regularly, and revoke them immediately in the provider console if you suspect a leak.

    4. Backup and recovery drill

    Backup is completed in three steps:

    1. Full backup: Copy the entire vault folder to an external drive or another device.
    2. Version control: Run git init in the vault and create a .gitignore file to exclude configuration files under .obsidian/plugins/ that may contain keys, as well as sensitive directories you do not want to back up.
    3. Regular snapshots: Use the built-in Time Machine or a similar tool at least once a week.

    To verify that backups are effective, perform a recovery drill:

    1. Copy the vault to a temporary location and delete a few test notes.
    2. Restore those notes from the backup.
    3. Open the restored vault with Obsidian and confirm that the note content is complete, plugins can be enabled normally, and AI retrieval results match the original.

    After the drill passes, apply this process to your production knowledge base.

    After deployment, it is recommended to run with non-sensitive notes for one to two weeks and observe answer quality, cost, and errors. After confirming stability, gradually add more notes.