EN ▾
Get API key

Troubleshooting Hallucinations in Uncensored Code Generation

When using an uncensored model for code generation, you often trade safety filters for raw creative freedom. This freedom can lead to plausible but incorrect code or ignored constraints, a phenomenon similar to the debates around specific system directives. This guide helps you troubleshoot these risks and prioritize output control.

Futuristic developer workspace with code and uncensored mask symbol

Understanding Hallucinations in Uncensored Models

When you switch from a standard safety-aligned model to an uncensored one, you remove the guardrails that typically prevent the model from making up facts or code structures. The model uncensored is designed to generate text without refusing lawful adult, fictional, or controversial topics. This means it will not stop itself from inventing a library that does not exist or a function signature that is syntactically incorrect if the context suggests it might be plausible.

Hallucinations in code often manifest as functions that look correct but have missing imports or incorrect argument types. Because the model is not penalized for being wrong in the same way a safety-tuned model might be, it may confidently output code that runs but produces unexpected results. This is not a bug but a feature of the uncensored design, which prioritizes raw output over factual accuracy.

To troubleshoot this, you must assume that every line of code generated is a hypothesis rather than a fact. You need to verify imports, check for deprecated methods, and ensure that the logic flows correctly. The model does not have a built-in compiler, so it cannot know if your code will actually run until you test it. This shifts the burden of verification from the model to you, the developer.

It is also important to note that the model does not have memory of previous interactions beyond the context window. If you ask for a specific implementation style in one turn, it may forget that style in the next turn if the context is too long. This can lead to inconsistent code quality across a large project. You need to be consistent in your prompts to get consistent results.

The Role of Temperature and Seed

One of the most effective ways to reduce hallucinations is to adjust the temperature parameter. A lower temperature makes the model more deterministic, meaning it will choose the most likely next token rather than exploring random possibilities. For code generation, a lower temperature often results in more standard and less creative code, which can reduce the likelihood of inventing non-existent functions.

However, lowering the temperature too much can make the code rigid and less adaptable to complex problems. You need to find a balance that allows for creativity while maintaining accuracy. The seed parameter can also be used to ensure that the same prompt produces the same output every time, which is useful for debugging and testing. If you get a hallucination, you can reproduce it exactly by using the same seed.

Another parameter to consider is top_p, which controls the diversity of the output. A lower top_p value restricts the model to the most probable tokens, which can further reduce hallucinations. You can experiment with different combinations of temperature and top_p to find the sweet spot for your specific use case.

Remember that these parameters affect the model's behavior globally, not just for a single request. If you are generating code for a large project, you need to ensure that the settings are consistent across all your API calls. This helps maintain a uniform style and reduces the risk of inconsistent hallucinations.

Enforcing Structure with JSON Mode

One of the best ways to mitigate hallucinations is to force the model to output data in a structured format. The API supports JSON mode, which ensures that the output is valid JSON. This is particularly useful for code generation because you can define a schema for the code structure, such as the function name, arguments, and return type.

By using JSON mode, you can ensure that the model does not output extraneous text or explanations that might confuse your parser. The model is constrained to output only the data that fits the schema, which reduces the risk of hallucinations in the code structure. You can also use function calling to define specific functions that the model can call, which further restricts the output to known entities.

This approach is especially useful when you are integrating the model into a larger application. You can parse the JSON output and execute the code safely, knowing that the structure is valid. This reduces the need for manual validation and makes the integration more reliable. It also allows you to handle errors more gracefully, as you can catch JSON parsing errors before they affect your application.

However, JSON mode does not guarantee that the code inside the JSON is correct. It only ensures that the structure is valid. You still need to verify the logic and syntax of the code. But by reducing the noise and enforcing structure, you make it easier to spot and fix hallucinations.

When Our API Is Not the Right Choice

While our API is powerful for uncensored code generation, it is not the right choice for every scenario. If you need a model that guarantees factual accuracy and has a strong safety filter, our uncensored model may produce too many hallucinations. For example, if you are generating code for a critical system where a single error could cause a major failure, you might prefer a model that is more conservative and less creative.

Our API serves a single uncensored open-weight text model with a 100,000-token context window. It does not support embeddings, image generation, or fine-tuning. If you need these features, our API is not the right choice. Additionally, if you require a model that is specifically trained for a particular domain, such as medical or legal code, our general-purpose uncensored model may not be specialized enough.

The API is priced at $0.15 per 1M input tokens and $0.50 per 1M output tokens. If you are generating a large volume of code with high token usage, the costs can add up quickly. You need to monitor your token usage and budget accordingly. If you need a free tier or a subscription model, our API may not fit your needs.

Finally, if you need a model that can handle multiple modalities, such as text, image, and audio, our API is not the right choice. It is a text-only API. If you need a multi-modal solution, you should look for a vendor that offers those capabilities. Our API is designed for developers who want raw, uncensored text output for code generation.

Addressing Community Concerns on Constraints

Developers often discuss issues where models ignore constraints or generate unexpected content, similar to the debates about specific directives like the goblin rule. In these discussions, users report that models may bring up unrelated topics or ignore explicit instructions to avoid certain creatures or concepts. This highlights the challenge of controlling an uncensored model.

The core issue is that uncensored models are designed to be less restrictive. They may not adhere strictly to negative constraints, such as never talk about goblins. This can lead to hallucinations where the model invents content that violates the constraint. To address this, you need to be very explicit in your prompts and use multiple strategies to enforce constraints.

One strategy is to use few-shot learning, where you provide examples of the desired output and the unwanted output. This helps the model understand the pattern you want to enforce. You can also use system prompts to set the tone and constraints for the model. However, no single strategy is foolproof, and you may need to experiment with different combinations to find what works best.

Another approach is to post-process the output. You can scan the generated code for keywords or patterns that indicate a hallucination and filter them out. This adds a layer of control but requires additional development effort. It is a trade-off between automation and accuracy.

Monitoring and Budgeting Token Usage

Since our API charges by token usage, it is important to monitor your usage to avoid unexpected costs. The API charges $0.15 per 1M input tokens and $0.50 per 1M output tokens. Errors and refusals are free, which can help you save money if you are testing different prompts. However, successful requests are charged based on the number of tokens used.

You can monitor your usage by checking the response headers, which include the token counts for input and output. This allows you to track how much you are spending on each request. You can also set up alerts or budgets in your application to notify you when you are approaching your limit.

The API supports streaming over SSE, which allows you to receive the output in chunks. This can help you manage the response time and provide a better user experience. However, streaming does not reduce the token count, so you still need to monitor your usage carefully.

Remember that the context window is 100,000 tokens, which includes both the prompt and the completion. If you are sending large prompts, you may run out of space for the completion. This can lead to truncated outputs, which may contain hallucinations. You need to manage your context window carefully to ensure that you have enough space for the full response.

Integrating with Your Workflow

Integrating our API into your workflow requires a few steps. First, you need to sign up and get an API key. The key is shown immediately after signup, so you can start using it right away. You can sign up with Google or an email and password. The base URL for the API is https://api.getcodexapi.com/v1.

You can use the API to send chat completions, responses, and models. The API supports POST requests to /v1/chat/completions and /v1/responses, and GET requests to /v1/models. You can use the uncensored model ID to access the uncensored model.

The API supports various parameters, such as temperature, top_p, stop, and seed. You can use these parameters to control the output and reduce hallucinations. You can also use JSON mode and function calling to enforce structure.

Finally, you need to handle errors and rate limits. The API returns 401 for invalid keys, 402 for no credit, and 429 for rate limits. The rate limit is 300 requests per minute per key. You need to handle these errors gracefully in your application to ensure a smooth user experience.

Frequently asked questions

How do I reduce hallucinations in code generation?

Lower the temperature and top_p parameters to make the output more deterministic. Use JSON mode to enforce structure and reduce noise. Provide few-shot examples to guide the model.

What is the cost of using the uncensored API?

The API charges $0.15 per 1M input tokens and $0.50 per 1M output tokens. Errors and refusals are free. You pay only for real token usage.

Can I use the API for image generation?

No, the API is text-only. It does not support image, audio, or video generation. It serves a single uncensored open-weight text model.

How do I sign up for the API?

You can sign up with Google or an email and password. The API key is shown immediately after signup. You do not need a credit card to start.